Skip to content

Checks that all elements in the given 2nd iterable are members of the given 1st iterable

License

Notifications You must be signed in to change notification settings

justin-calleja/all-included-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

all-included-validator Build status NPM version License Semantic Release Js Standard Style

Install

npm i @justinc/all-included-validator

Demo

(look at tests for more examples)

// NOTE: run  `node repl.js` to see this in action:
const allIncluded = require('@justinc/all-included-validator')

const ids = [1, 2, 3, 4, 5]
const refs = [1, 2]
const nums = [2, 4, 2, 3, 1]
const over9000s = [9001, 9002]

const allIncludedInIds = allIncluded(ids)
const allIncludedInOver9000s = allIncluded(over9000s)

console.log(allIncludedInIds(refs)('The following refs are not in ids: '))
// [ true, [] ]

console.log(allIncludedInIds(nums)())
// [true, [] ]

const allOver9000sIncludedInIds = allIncludedInIds(over9000s)
console.log(allOver9000sIncludedInIds('The following are not in ids: '))
// [ false, [ new Error('The following are not in ids: 9001, 9002') ]]

console.log(allOver9000sIncludedInIds('Ids knows not of these: '))
// [ false, [ new Error('Ids knows not of these: 9001, 9002') ]]

console.log(allIncludedInOver9000s(ids)(`These ids aren't even over 9000: `))
// [ false, [ new Error(`These ids aren't even over 9000: : 1, 2, 3, 4, 5`) ]]

If you're OK with using Folktale Validation, you might be interested in combine-validations.

To use this package with combine-validations you will need to convert its output Tuple<Boolean, Array<Error>> to a Folktale Validation with something like this:

const Validation = require('data.validation')
const allIncluded = require('@justinc/all-included-validator')
const combineValidations = require('@justinc/combine-validations')

const { Success, Failure } = Validation

const asValidation = ([isValid, errors]) => isValid ? Success(true) : Failure(errors)

const ids = ['node1', 'node2', 'node3']
const dependencies = [ 'node1', 'node3' ]
const validation = combineValidations([
  asValidation(allIncluded(ids)(dependencies)())
])

Tutorials

TODO: A JSDoc 3 tutorial (re currying) needs to be injected in this README.md. For now there's only a link:

Modules

@justinc/all-included-validator
@justinc/jsdocs

This module houses JSDoc 3 type definitions which can be re-used in different packages.

@justinc/all-included-validator

@justinc/all-included-validator~allIncluded([iterableA], [iterableB], [errMsgPrefix]) ⇒ Tuple.<Boolean, Array.<Error>>

This function is curried. Checks that all elements in the given 2nd iterable are members of the given 1st iterable.

Kind: inner method of @justinc/all-included-validator
See: Tuple

Param Type Default Description
[iterableA] Iterable [] The 1st iterable
[iterableB] Iterable [] The 2nd iterable, whose elements should all be in the 1st iterable
[errMsgPrefix] String '' A string to prefix the error message (the err msg is any found 2nd iterable elems which are not in the 1st iterable)

@justinc/jsdocs

This module houses JSDoc 3 type definitions which can be re-used in different packages.

@justinc/jsdocs.Tuple : Array

The type Tuple is an Array of fixed length whose elements at specific indices are of the specified types.

Kind: static typedef of @justinc/jsdocs
Example

// A Tuple of arity (length) 2, whose first el is a Boolean and second el is
// an Array of Error:
Tuple<Boolean, Array<Error>>
// e.g. [ true, [] ]
// e.g. [ false, [ new Error('computer says no') ] ]

Example

// A Tuple of arity (length) 3, whose first el is a String, and both the
// second and third els are an Array of String:
Tuple<String, Array<String>, Array<String>>
// e.g. [ 'hello', [ 'world' ], [ 'goodbye', 'world' ] ]

About

Checks that all elements in the given 2nd iterable are members of the given 1st iterable

Resources

License

Stars

Watchers

Forks

Packages

No packages published