only-one-type

A simple module to check if all elements in an array/object/map/set are belong to only one primitive type


Keywords
primitive, iterable, boolean, array, map, object, set
License
MPL-2.0
Install
npm install only-one-type@0.2.0

Documentation

How to use

only-one-type is for checking if an Array/Set/Map/Object object
is composed with only one type of primitive type.

Important Does not take care if one or more values of the objects are an Array/Set/Map/Object itsel, so you can checking per example if all values of an Array object in an Array object...in any Array/Set/Map/Object objects are belongs to one primitive value.

With key-value paired Objects (Object instances, Map instances), check only the value.

Can be serve a Promise object so only-one-type can be used in asynchrone mode. the PromiseValue to deserve is the boolean value of the test if resolved, else is the throwed error if rejected.

Example :

const onlyOneType = require('only-one-type')

var arr = [1, 2, 3, [1, 2, 3, 4], [[[[1]]]] ]

onlyOneType(arr, 'number') // => true

// Promise mode

onlyOneType(arr, 'number', true).then( (bool) => console.log(bool))

API

onlyOneType(iterable, string, [boolean]) --> boolean | promise

  • iterable : an Iterable Object, like Array, Map, Set, Object (only enumerable object)

  • string : one of primitive value JS in string, like it returned by typeof . `'string' 'object' 'number' 'function' 'boolean' 'null' 'undefined'

  • boolean : if true, active the Promise mode.