Socket
Socket
Sign inDemoInstall

test-value

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

test-value - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

124

lib/test-value.js

@@ -1,70 +0,80 @@

"use strict";
var arrayify = require("array-back");
var t = require("typical");
'use strict'
var arrayify = require('array-back')
var t = require('typical')
/**
@module test-value
@example
var testValue = require("test-value");
*/
module.exports = testValue;
* @module test-value
* @example
* var testValue = require('test-value')
*/
module.exports = testValue
/**
@alias module:test-value
@param {any} - a value to test
@param {any} - the test query
@returns {boolean}
*/
function testValue(value, test){
if (t.isPlainObject(test) && t.isObject(value)){
return Object.keys(test).every(function(prop){
var queryValue = test[prop];
* @alias module:test-value
* @param {any} - a value to test
* @param {any} - the test query
* @returns {boolean}
*/
function testValue (value, test) {
if (t.isPlainObject(test) && t.isObject(value)) {
return Object.keys(test).every(function (prop) {
var queryValue = test[prop]
/* get flags */
var isNegated = false;
var isContains = false;
/* get flags */
var isNegated = false
var isContains = false
if (prop.charAt(0) === "!"){
isNegated = true;
} else if (prop.charAt(0) === "+") {
isContains = true;
}
if (prop.charAt(0) === '!') {
isNegated = true
} else if (prop.charAt(0) === '+') {
isContains = true
}
/* strip flag char */
prop = (isNegated || isContains) ? prop.slice(1) : prop;
var objectValue = value[prop];
/* strip flag char */
prop = (isNegated || isContains) ? prop.slice(1) : prop
var objectValue = value[prop]
if (isContains){
queryValue = arrayify(queryValue);
objectValue = arrayify(objectValue);
}
if (isContains) {
queryValue = arrayify(queryValue)
objectValue = arrayify(objectValue)
}
var result = testValue(objectValue, queryValue);
return isNegated ? !result : result;
});
} else if (Array.isArray(test)){
var tests = test;
if (!Array.isArray(value)) value = [ value ];
return value.some(function(val){
return tests.some(function(test){
return testValue(val, test);
});
});
var result = testValue(objectValue, queryValue)
return isNegated ? !result : result
})
} else if (Array.isArray(test)) {
var tests = test
if (!Array.isArray(value)) value = [ value ]
return value.some(function (val) {
return tests.some(function (test) {
return testValue(val, test)
})
})
/*
regexes queries will always return `false` for `null`, `undefined`, `NaN`.
This is to prevent a query like `/.+/` matching the string `undefined`.
*/
} else if (test instanceof RegExp){
if ([ "boolean", "string", "number" ].indexOf(typeof value) === -1){
return false;
} else {
return test.test(value);
}
} else if (typeof test === "function"){
return test(value);
/*
regexes queries will always return `false` for `null`, `undefined`, `NaN`.
This is to prevent a query like `/.+/` matching the string `undefined`.
*/
} else if (test instanceof RegExp) {
if ([ 'boolean', 'string', 'number' ].indexOf(typeof value) === -1) {
return false
} else {
return test === value;
return test.test(value)
}
} else if (typeof test === 'function') {
return test(value)
} else {
return test === value
}
}
/**
* Returns a callback suitable for use by `Array` methods like `some`, `filter`, `find` etc.
* @param {any} - the test query
* @returns {function}
*/
testValue.cb = function (test) {
return function (value) {
return testValue(value, test)
}
}
{
"name": "test-value",
"author": "Lloyd Brookes <75pound@gmail.com>",
"version": "1.0.1",
"description": "test-value",
"version": "1.1.0",
"description": "Test a value in a variety of ways",
"repository": "https://github.com/75lb/test-value.git",

@@ -14,4 +14,4 @@ "license": "MIT",

"dependencies": {
"array-back": "^1",
"typical": "^2.3.0"
"array-back": "^1.0.2",
"typical": "^2.4.2"
},

@@ -23,5 +23,5 @@ "scripts": {

"devDependencies": {
"jsdoc-to-markdown": "^1.1.1",
"tape": "^4.0.0"
"jsdoc-to-markdown": "^1.3.3",
"tape": "^4.5.0"
}
}
[![view on npm](http://img.shields.io/npm/v/test-value.svg)](https://www.npmjs.org/package/test-value)
[![npm module downloads per month](http://img.shields.io/npm/dm/test-value.svg)](https://www.npmjs.org/package/test-value)
[![npm module downloads](http://img.shields.io/npm/dt/test-value.svg)](https://www.npmjs.org/package/test-value)
[![Build Status](https://travis-ci.org/75lb/test-value.svg?branch=master)](https://travis-ci.org/75lb/test-value)
[![Dependency Status](https://david-dm.org/75lb/test-value.svg)](https://david-dm.org/75lb/test-value)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)

@@ -10,4 +11,9 @@ <a name="module_test-value"></a>

```js
var testValue = require("test-value");
var testValue = require('test-value')
```
* [test-value](#module_test-value)
* [testValue(value, test)](#exp_module_test-value--testValue) ⇒ <code>boolean</code> ⏏
* [.cb(test)](#module_test-value--testValue.cb) ⇒ <code>function</code>
<a name="exp_module_test-value--testValue"></a>

@@ -22,5 +28,15 @@ ### testValue(value, test) ⇒ <code>boolean</code> ⏏

<a name="module_test-value--testValue.cb"></a>
#### testValue.cb(test) ⇒ <code>function</code>
Returns a callback suitable for use by `Array` methods like `some`, `filter`, `find` etc.
**Kind**: static method of <code>[testValue](#exp_module_test-value--testValue)</code>
| Param | Type | Description |
| --- | --- | --- |
| test | <code>any</code> | the test query |
* * *
&copy; 2015 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).
&copy; 2015-16 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).

@@ -1,222 +0,232 @@

var test = require("tape");
var testValue = require("../");
var test = require('tape')
var testValue = require('../')
function TestClass(){
this.one = 1;
function TestClass () {
this.one = 1
}
var testClass = new TestClass();
var testClass = new TestClass()
var fixture = {
result: "clive",
hater: true,
colour: "red-ish",
deep: {
name: "Zhana",
favourite: {
colour: [ "white", "red" ]
},
arr: [ 1, 2, 3 ]
result: 'clive',
hater: true,
colour: 'red-ish',
deep: {
name: 'Zhana',
favourite: {
colour: [ 'white', 'red' ]
},
nullVal: null,
boolTrue: true,
number: 5,
testClass: testClass,
arr: [ 1, 2, 3 ],
arrObjects: [
{ number: 1 },
{ number: 2 }
]
};
arr: [ 1, 2, 3 ]
},
nullVal: null,
boolTrue: true,
number: 5,
testClass: testClass,
arr: [ 1, 2, 3 ],
arrObjects: [
{ number: 1 },
{ number: 2 }
]
}
test(".exists(obj, { property: primative })", function(t){
t.strictEqual(testValue(fixture, { result: "clive" }), true);
t.strictEqual(testValue(fixture, { hater: true }), true);
t.strictEqual(testValue(fixture, { result: "clive", hater: true }), true);
t.strictEqual(testValue(fixture, { ibe: true }), false);
t.end();
});
test('.exists(obj, { property: primative })', function (t) {
t.strictEqual(testValue(fixture, { result: 'clive' }), true)
t.strictEqual(testValue(fixture, { hater: true }), true)
t.strictEqual(testValue(fixture, { result: 'clive', hater: true }), true)
t.strictEqual(testValue(fixture, { ibe: true }), false)
t.end()
})
test(".exists(obj, { !property: primative })", function(t){
t.strictEqual(testValue(fixture, { "!result": "clive" }), false);
t.strictEqual(testValue(fixture, { "!result": "ian" }), true);
t.strictEqual(testValue(fixture, { "!result": "ian", "!hater": false }), true);
t.end();
});
test('.exists(obj, { !property: primative })', function (t) {
t.strictEqual(testValue(fixture, { '!result': 'clive' }), false)
t.strictEqual(testValue(fixture, { '!result': 'ian' }), true)
t.strictEqual(testValue(fixture, { '!result': 'ian', '!hater': false }), true)
t.end()
})
test(".exists(obj, { property: primative[] })", function(t){
t.strictEqual(testValue(fixture, { arr: [ 1, 2, 3 ] }), true);
t.strictEqual(testValue(fixture, { arr: [ /1/ ] }), true);
t.strictEqual(testValue(fixture, { arr: [ /4/ ] }), false);
t.strictEqual(testValue(fixture, { colour: [ 1, 2, 3 ] }), false, "querying a string with array");
t.strictEqual(testValue(fixture, { undefinedProperty: [ 1, 2, 3 ] }), false, "querying undefined property");
t.strictEqual(testValue(fixture, { undefinedProperty: [ undefined ] }), true);
t.strictEqual(testValue(fixture, { undefinedProperty: [ null ] }), false);
t.end();
});
test('.exists(obj, { property: primative[] })', function (t) {
t.strictEqual(testValue(fixture, { arr: [ 1, 2, 3 ] }), true)
t.strictEqual(testValue(fixture, { arr: [ /1/ ] }), true)
t.strictEqual(testValue(fixture, { arr: [ /4/ ] }), false)
t.strictEqual(testValue(fixture, { colour: [ 1, 2, 3 ] }), false, 'querying a string with array')
t.strictEqual(testValue(fixture, { undefinedProperty: [ 1, 2, 3 ] }), false, 'querying undefined property')
t.strictEqual(testValue(fixture, { undefinedProperty: [ undefined ] }), true)
t.strictEqual(testValue(fixture, { undefinedProperty: [ null ] }), false)
t.end()
})
test(".exists(obj, { property: { property: primative[] } })", function(t){
t.strictEqual(testValue(fixture, { deep: { arr: [ 1, 2 ] } }), true);
t.strictEqual(testValue(fixture, { deep: { arr: [ 3, 4 ] } }), true);
t.strictEqual(testValue(fixture, { deep: { favourite: { colour: [ "white", "red" ] } } }), true);
t.end();
});
test('.exists(obj, { property: { property: primative[] } })', function (t) {
t.strictEqual(testValue(fixture, { deep: { arr: [ 1, 2 ] } }), true)
t.strictEqual(testValue(fixture, { deep: { arr: [ 3, 4 ] } }), true)
t.strictEqual(testValue(fixture, { deep: { favourite: { colour: [ 'white', 'red' ] } } }), true)
t.end()
})
test(".exists(obj, { property: undefined, property: regex })", function(t){
t.strictEqual(testValue(fixture.deep, { undefinedProperty: undefined, name: /.+/ }), true);
t.end();
});
test('.exists(obj, { property: undefined, property: regex })', function (t) {
t.strictEqual(testValue(fixture.deep, { undefinedProperty: undefined, name: /.+/ }), true)
t.end()
})
test(".exists(obj, { property: /regex/ })", function(t){
t.strictEqual(testValue(fixture, { colour: /red/ }), true);
t.strictEqual(testValue(fixture, { colour: /black/ }), false);
t.strictEqual(testValue(fixture, { colour: /RED/i }), true);
t.strictEqual(testValue(fixture, { colour: /.+/ }), true);
t.strictEqual(testValue(fixture, { undefinedProperty: /.+/ }), false, "testing undefined val");
t.strictEqual(testValue(fixture, { deep: /.+/ }), false, "testing an object val");
t.strictEqual(testValue(fixture, { nullVal: /.+/ }), false, "testing a null val");
t.strictEqual(testValue(fixture, { boolTrue: /true/ }), true, "testing a boolean val");
t.strictEqual(testValue(fixture, { boolTrue: /addf/ }), false, "testing a boolean val");
t.end();
});
test('.exists(obj, { property: /regex/ })', function (t) {
t.strictEqual(testValue(fixture, { colour: /red/ }), true)
t.strictEqual(testValue(fixture, { colour: /black/ }), false)
t.strictEqual(testValue(fixture, { colour: /RED/i }), true)
t.strictEqual(testValue(fixture, { colour: /.+/ }), true)
t.strictEqual(testValue(fixture, { undefinedProperty: /.+/ }), false, 'testing undefined val')
t.strictEqual(testValue(fixture, { deep: /.+/ }), false, 'testing an object val')
t.strictEqual(testValue(fixture, { nullVal: /.+/ }), false, 'testing a null val')
t.strictEqual(testValue(fixture, { boolTrue: /true/ }), true, 'testing a boolean val')
t.strictEqual(testValue(fixture, { boolTrue: /addf/ }), false, 'testing a boolean val')
t.end()
})
test(".exists(obj, { !property: /regex/ })", function(t){
t.strictEqual(testValue(fixture, { "!colour": /red/ }), false);
t.strictEqual(testValue(fixture, { "!colour": /black/ }), true);
t.strictEqual(testValue(fixture, { "!colour": /blue/ }), true);
t.end();
});
test('.exists(obj, { !property: /regex/ })', function (t) {
t.strictEqual(testValue(fixture, { '!colour': /red/ }), false)
t.strictEqual(testValue(fixture, { '!colour': /black/ }), true)
t.strictEqual(testValue(fixture, { '!colour': /blue/ }), true)
t.end()
})
test(".exists(obj, { property: function })", function(t){
t.strictEqual(testValue(fixture, { number: function(n){ return n < 4; }}), false, "< 4");
t.strictEqual(testValue(fixture, { number: function(n){ return n < 10; }}), true, "< 10");
t.end();
});
test('.exists(obj, { property: function })', function (t) {
t.strictEqual(testValue(fixture, { number: function (n) { return n < 4 } }), false, '< 4')
t.strictEqual(testValue(fixture, { number: function (n) { return n < 10 } }), true, '< 10')
t.end()
})
test(".exists(obj, { !property: function })", function(t){
t.strictEqual(testValue(fixture, { "!number": function(n){ return n < 10; }}), false, "< 10");
t.end();
});
test('.exists(obj, { !property: function })', function (t) {
t.strictEqual(testValue(fixture, { '!number': function (n) { return n < 10 } }), false, '< 10')
t.end()
})
test(".exists(obj, { property: object })", function(t){
t.strictEqual(testValue(fixture, { testClass: { one: 1 } }), true, "querying a plain object");
t.strictEqual(testValue(fixture, { testClass: testClass }), true, "querying an object instance");
t.end();
});
test('.exists(obj, { property: object })', function (t) {
t.strictEqual(testValue(fixture, { testClass: { one: 1 } }), true, 'querying a plain object')
t.strictEqual(testValue(fixture, { testClass: testClass }), true, 'querying an object instance')
t.end()
})
test('.exists(obj, { +property: primitive })', function (t) {
t.strictEqual(testValue(fixture, { arr: 1 }), false)
t.strictEqual(testValue(fixture, { '+arr': 1 }), true)
t.end()
})
test(".exists(obj, { +property: primitive })", function(t){
t.strictEqual(testValue(fixture, { arr: 1 }), false);
t.strictEqual(testValue(fixture, { "+arr": 1 }), true);
t.end();
});
test('.exists(obj, { property. { +property: query } })', function (t) {
t.strictEqual(testValue(fixture, { deep: { favourite: { '+colour': 'red' } } }), true)
t.strictEqual(testValue(fixture, { deep: { favourite: { '+colour': /red/ } } }), true)
t.strictEqual(testValue(fixture, { deep: { favourite: { '+colour': function (c) {
return c === 'red'
} } } }), true)
t.strictEqual(testValue(fixture, { deep: { favourite: { '+colour': /green/ } } }), false)
t.end()
})
test(".exists(obj, { property. { +property: query } })", function(t){
t.strictEqual(testValue(fixture, { deep: { favourite: { "+colour": "red" } } }), true);
t.strictEqual(testValue(fixture, { deep: { favourite: { "+colour": /red/ } } }), true);
t.strictEqual(testValue(fixture, { deep: { favourite: { "+colour": function(c){
return c === "red";
} } } }), true);
t.strictEqual(testValue(fixture, { deep: { favourite: { "+colour": /green/ } } }), false);
t.end();
});
test('.exists(obj, { +property: query })', function (t) {
t.strictEqual(testValue(fixture, { arrObjects: { number: 1 } }), false)
t.strictEqual(testValue(fixture, { '+arrObjects': { number: 1 } }), true)
t.end()
})
test(".exists(obj, { +property: query })", function(t){
t.strictEqual(testValue(fixture, { arrObjects: { number: 1 } }), false);
t.strictEqual(testValue(fixture, { "+arrObjects": { number: 1 } }), true);
t.end();
});
test("object deep exists, summary", function(t){
var query = {
test('object deep exists, summary', function (t) {
var query = {
one: {
one: {
three: 'three',
'!four': 'four'
},
two: {
one: {
one: {
three: "three",
"!four": "four"
},
two: {
one: {
one: "one"
},
"!two": undefined,
"!three": [ { "!one": { "!one": "110" } } ]
}
}
};
one: 'one'
},
'!two': undefined,
'!three': [ { '!one': { '!one': '110' } } ]
}
}
}
var obj1 = {
var obj1 = {
one: {
one: {
one: 'one',
two: 'two',
three: 'three'
},
two: {
one: {
one: {
one: "one",
two: "two",
three: "three"
},
two: {
one: {
one: "one"
},
two: 2
}
}
};
one: 'one'
},
two: 2
}
}
}
var obj2 = {
var obj2 = {
one: {
one: {
one: 'one',
two: 'two'
},
two: {
one: {
one: {
one: "one",
two: "two"
},
two: {
one: {
one: "one"
},
two: 2
}
}
};
one: 'one'
},
two: 2
}
}
}
var obj3 = {
var obj3 = {
one: {
one: {
one: 'one',
two: 'two',
three: 'three'
},
two: {
one: {
one: {
one: "one",
two: "two",
three: "three"
},
two: {
one: {
one: "one"
},
two: 2,
three: [
{ one: { one: "100" } },
{ one: { one: "110" } }
]
}
}
};
one: 'one'
},
two: 2,
three: [
{ one: { one: '100' } },
{ one: { one: '110' } }
]
}
}
}
var obj4 = {
var obj4 = {
one: {
one: {
one: 'one',
two: 'two',
three: 'three'
},
two: {
one: {
one: {
one: "one",
two: "two",
three: "three"
},
two: {
one: {
one: "one"
},
two: 2,
three: [
{ one: { one: "100" } }
]
}
}
};
one: 'one'
},
two: 2,
three: [
{ one: { one: '100' } }
]
}
}
}
t.strictEqual(testValue(obj1, query), true, "true obj1");
t.strictEqual(testValue(obj2, query), false, "false obj2");
t.strictEqual(testValue(obj3, query), false, "false in obj3");
t.strictEqual(testValue(obj4, query), true, "true in obj4");
t.end();
});
t.strictEqual(testValue(obj1, query), true, 'true obj1')
t.strictEqual(testValue(obj2, query), false, 'false obj2')
t.strictEqual(testValue(obj3, query), false, 'false in obj3')
t.strictEqual(testValue(obj4, query), true, 'true in obj4')
t.end()
})
test('testValue.cb({ property: primative })', function (t) {
var arr = [
{ num: 1 }, { num: 2 }, { num: 3 }
]
t.strictEqual(arr.some(testValue.cb({ num: 2 })), true)
t.strictEqual(arr.some(testValue.cb({ num: 4 })), false)
t.deepEqual(arr.filter(testValue.cb({ num: 2 })), [ { num: 2 } ])
t.deepEqual(arr.filter(testValue.cb({ num: 4 })), [])
t.end()
})

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc