Comparing version 1.0.1 to 1.1.0
@@ -63,2 +63,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
var objIdentifier = '[object Object]'; | ||
var dateIdentifier = '[object Date]'; | ||
@@ -104,4 +105,6 @@ var curry = function curry(fn) { | ||
var protoToString = Object.prototype.toString; | ||
var isObject = function isObject(obj) { | ||
return Object.prototype.toString.call(obj) === objIdentifier; | ||
return protoToString.call(obj) === objIdentifier; | ||
}; | ||
@@ -130,2 +133,59 @@ | ||
var isArrayEqual = function isArrayEqual(arr1, arr2) { | ||
if (arr1.length !== arr2.length) { | ||
return false; | ||
} | ||
for (var i = 0; i < arr1.length; i++) { | ||
if (arr1[i] !== arr2[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
// TODO: May be a create a looper function which calls itself when it comes across a nested object | ||
var isEqual = function isEqual(val1, val2) { | ||
if (val1 === val2) { | ||
return true; | ||
} | ||
var val1Proto = protoToString.call(val1); | ||
var val2Proto = protoToString.call(val2); | ||
if (val1Proto !== val2Proto) { | ||
return false; | ||
} | ||
if (Array.isArray(val1)) { | ||
return isArrayEqual(val1, val2); | ||
} | ||
if (val1Proto === dateIdentifier) { | ||
if (typeof val1.getTime === 'function' && typeof val2.getTime === 'function') { | ||
return val1.getTime() === val2.getTime(); | ||
} | ||
return false; | ||
} | ||
return false; | ||
}; | ||
var comparator = function comparator(key, value, obj) { | ||
if (typeof value === 'function' && value(obj[key])) { | ||
return key; | ||
} | ||
if (isEqual(obj[key], value)) { | ||
return key; | ||
} | ||
return void 0; | ||
}; | ||
var findKeyByValue = curry(function (value, obj) { | ||
return Object.keys(obj).find(function (key) { | ||
return comparator(key, value, obj); | ||
}); | ||
}); | ||
exports.default = { | ||
@@ -137,3 +197,5 @@ map: map, | ||
extend: extend, | ||
omit: omit | ||
omit: omit, | ||
findKeyByValue: findKeyByValue, | ||
isArrayEqual: isArrayEqual | ||
}; | ||
@@ -140,0 +202,0 @@ module.exports = exports['default']; |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("osm",[],t):"object"==typeof exports?exports.osm=t():e.osm=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n="[object Object]",r=function(e){var t=e.length,n=function r(){for(var n=arguments.length,o=Array(n),u=0;u<n;u++)o[u]=arguments[u];return o.length<t?function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return r.apply(void 0,o.concat(t))}:e.apply(void 0,o)};return n},o=r(function(e,t){return Object.keys(t).reduce(function(n,r){return n[r]=e(t[r]),n},{})}),u=r(function(e,t){return Object.keys(t).filter(function(n){return e(t[n],n)}).reduce(function(e,n){return e[n]=t[n],e},{})}),c=function(e){return Object.prototype.toString.call(e)===n},f=function(e){return Object.create(e)},i=function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return t.reduce(function(e,t){return Object.assign(e,t),e},{})},a=function(e,t){return u(function(e,n){return t.indexOf(n)===-1},e)};t["default"]={map:o,filter:u,isObject:c,clone:f,extend:i,omit:a},e.exports=t["default"]}])}); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("osm",[],e):"object"==typeof exports?exports.osm=e():t.osm=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n="[object Object]",r="[object Date]",o=function(t){var e=t.length,n=function r(){for(var n=arguments.length,o=Array(n),u=0;u<n;u++)o[u]=arguments[u];return o.length<e?function(){for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];return r.apply(void 0,o.concat(e))}:t.apply(void 0,o)};return n},u=o(function(t,e){return Object.keys(e).reduce(function(n,r){return n[r]=t(e[r]),n},{})}),i=o(function(t,e){return Object.keys(e).filter(function(n){return t(e[n],n)}).reduce(function(t,n){return t[n]=e[n],t},{})}),c=Object.prototype.toString,f=function(t){return c.call(t)===n},a=function(t){return Object.create(t)},l=function(){for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];return e.reduce(function(t,e){return Object.assign(t,e),t},{})},p=function(t,e){return i(function(t,n){return e.indexOf(n)===-1},t)},d=function(t,e){if(t.length!==e.length)return!1;for(var n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0},s=function(t,e){if(t===e)return!0;var n=c.call(t),o=c.call(e);return n===o&&(Array.isArray(t)?d(t,e):n===r&&("function"==typeof t.getTime&&"function"==typeof e.getTime&&t.getTime()===e.getTime()))},y=function(t,e,n){return"function"==typeof e&&e(n[t])?t:s(n[t],e)?t:void 0},b=o(function(t,e){return Object.keys(e).find(function(n){return y(n,t,e)})});e["default"]={map:u,filter:i,isObject:f,clone:a,extend:l,omit:p,findKeyByValue:b,isArrayEqual:d},t.exports=e["default"]}])}); | ||
//# sourceMappingURL=osm.min.js.map |
@@ -54,3 +54,3 @@ { | ||
}, | ||
"version": "1.0.1" | ||
"version": "1.1.0" | ||
} |
@@ -128,1 +128,29 @@ # Osm.js | ||
``` | ||
### findKeyByValue | ||
```javascript | ||
osm.findKeyByValue(val, obj); | ||
``` | ||
Returns the first key which matches the pass value. If no match found, then undefined is returned. | ||
Note: If the `val` is `NaN` or an `object` then `undefined` is returned. | ||
```javascript | ||
const obj = { | ||
'arrayVal': [1, 2, 3], | ||
'boolean': false, | ||
'string': 'test', | ||
'nullVal': null, | ||
'notDefined': undefined, | ||
'notANumber': NaN, | ||
'dateVal': new Date('2016-01-01'), | ||
'secondBool': false | ||
} | ||
osm.findKeyByValue([1,2,3], obj); //returns 'arrayVal' | ||
osm.findKeyByValue(false, obj); //returns 'boolean' | ||
osm.findKeyByValue('test', obj); //returns 'string' | ||
osm.findKeyByValue([null, obj); //returns 'nullVal' | ||
osm.findKeyByValue(undefined, obj); //returns 'notDefined' | ||
osm.findKeyByValue(NaN, obj); //returns undefined | ||
osm.findKeyByValue(new Date('2016-01-01'), obj); //returns dateVal | ||
``` |
const objIdentifier = '[object Object]'; | ||
const dateIdentifier = '[object Date]'; | ||
@@ -33,4 +34,6 @@ const curry = (fn) => { | ||
const isObject = (obj) => Object.prototype.toString.call(obj) === objIdentifier; | ||
const protoToString = Object.prototype.toString; | ||
const isObject = (obj) => protoToString.call(obj) === objIdentifier; | ||
const clone = (obj) => Object.create(obj); | ||
@@ -47,2 +50,58 @@ | ||
const isArrayEqual = (arr1, arr2) => { | ||
if (arr1.length !== arr2.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < arr1.length; i++) { | ||
if (arr1[i] !== arr2[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
// TODO: May be a create a looper function which calls itself when it comes across a nested object | ||
const isEqual = (val1, val2) => { | ||
if (val1 === val2) { | ||
return true; | ||
} | ||
const val1Proto = protoToString.call(val1); | ||
const val2Proto = protoToString.call(val2); | ||
if (val1Proto !== val2Proto) { | ||
return false; | ||
} | ||
if (Array.isArray(val1)) { | ||
return isArrayEqual(val1, val2); | ||
} | ||
if (val1Proto === dateIdentifier) { | ||
if (typeof val1.getTime === 'function' && typeof val2.getTime === 'function') { | ||
return val1.getTime() === val2.getTime(); | ||
} | ||
return false; | ||
} | ||
return false; | ||
}; | ||
const comparator = (key, value, obj) => { | ||
if (typeof value === 'function' && value(obj[key])) { | ||
return key; | ||
} | ||
if (isEqual(obj[key], value)) { | ||
return key; | ||
} | ||
return void 0; | ||
}; | ||
const findKeyByValue = curry((value, obj) => { | ||
return Object.keys(obj) | ||
.find(key => comparator(key, value, obj)); | ||
}); | ||
export default { | ||
@@ -54,3 +113,5 @@ map, | ||
extend, | ||
omit | ||
omit, | ||
findKeyByValue, | ||
isArrayEqual | ||
}; |
@@ -23,2 +23,4 @@ import {expect, assert} from 'chai'; | ||
const predicate = score => score > 1; | ||
const truthy = (val) => !!val; | ||
const falsey = (val) => !val; | ||
@@ -38,2 +40,18 @@ describe('osm', () => { | ||
}); | ||
it('should only iterate over the own properties of the object', () => { | ||
let proto = { | ||
protoOwned: true | ||
}, | ||
testObj = Object.create(proto); | ||
testObj.myOwn = true; | ||
let result = osm.map((v) => v, testObj), | ||
expected = { | ||
myOwn: true | ||
}; | ||
expect(result).to.eql(expected); | ||
expect(result.hasOwnProperty('myOwn')).to.be.true; | ||
expect(result.hasOwnProperty('protoOwned')).to.be.false; | ||
}) | ||
}); | ||
@@ -89,4 +107,16 @@ | ||
describe('clone', () => { | ||
it('should create a shallow clone of the object', () => { | ||
expect(osm.clone(scores)).to.eql(scores); | ||
it('should create a deep clone of the object', () => { | ||
let animal = { | ||
name: 'Toby', | ||
type: 'Dog', | ||
tricks: { | ||
perfected: ['Sit', 'Lie Down', 'Rollover', 'Circle'], | ||
learning: ['Handshake'], | ||
teacher: { | ||
name: 'Mr. Awesome' | ||
} | ||
} | ||
}; | ||
expect(osm.clone(animal)).to.eql(animal); | ||
}); | ||
@@ -113,2 +143,265 @@ }); | ||
}); | ||
describe('isArrayEqual', () => { | ||
it('should return true when arrays both arrays point to same spot in memory', () => { | ||
const array = [1]; | ||
expect(osm.isArrayEqual(array, array)).to.be.true; | ||
}); | ||
it('should return true when arrays both arrays contain the same elements', () => { | ||
const array = [1]; | ||
const array2 = [1]; | ||
expect(osm.isArrayEqual(array, array2)).to.be.true; | ||
}); | ||
it(`should return false when arrays both arrays don't have the same length`, () => { | ||
const array = [1]; | ||
const array2 = [1,2]; | ||
expect(osm.isArrayEqual(array, array2)).to.be.false; | ||
}); | ||
it(`should return false when arrays both arrays don't contain same elements`, () => { | ||
const array = [1,3]; | ||
const array2 = [1,2]; | ||
expect(osm.isArrayEqual(array, array2)).to.be.false; | ||
}); | ||
}); | ||
describe('findKeyByValue', () => { | ||
it('should return a function when the object is not provided', () => { | ||
assert.isFunction(osm.findKeyByValue(truthy)); | ||
}); | ||
it('should return undefined when no keys in the object match the value returned by the function', () => { | ||
const fn = osm.findKeyByValue(falsey); | ||
const curriedVal = fn(scores); | ||
expect(curriedVal).to.eql(undefined); | ||
const returnVal = osm.findKeyByValue(falsey, scores); | ||
expect(returnVal).to.eql(undefined); | ||
}); | ||
it('should return undefined when no keys in the object match the value', () => { | ||
const fn = osm.findKeyByValue(7); | ||
const curriedVal = fn(scores); | ||
expect(curriedVal).to.eql(undefined); | ||
const returnVal = osm.findKeyByValue(7, scores); | ||
expect(returnVal).to.eql(undefined); | ||
}); | ||
it('should return the first key that matches the value returned by the function', () => { | ||
const expected = 'Jason'; | ||
const actual = osm.findKeyByValue(predicate, scores); | ||
expect(actual).to.eql(expected); | ||
const curried = osm.findKeyByValue(predicate); | ||
expect(curried(scores)).to.eql(expected); | ||
}); | ||
it('should return the first key that matches the value', () => { | ||
const expected = 'Sam'; | ||
const actual = osm.findKeyByValue(3, scores); | ||
expect(actual).to.eql(expected); | ||
const curried = osm.findKeyByValue(3); | ||
expect(curried(scores)).to.eql(expected); | ||
}); | ||
it('should return the first key that matches the value of an array', () => { | ||
const compareArray = [1, 2, 3]; | ||
const expected = 'arrayVal'; | ||
const obj = { | ||
arrayVal: [1, 2, 3], | ||
'boolean': false, | ||
'string': 'test', | ||
'innerObj': { | ||
innerKey: 'innerVal' | ||
}, | ||
'nullVal': null, | ||
'notDefined': undefined, | ||
'notANumber': NaN, | ||
'dateVal': new Date('2016-01-01') | ||
} | ||
let actual = osm.findKeyByValue(compareArray, obj); | ||
expect(actual).to.eql(expected); | ||
actual = osm.findKeyByValue([], obj); | ||
expect(actual).to.eql(undefined); | ||
const curried = osm.findKeyByValue(compareArray); | ||
expect(curried(obj)).to.eql(expected); | ||
}); | ||
it('should return the first key that matches the value of a boolen', () => { | ||
const expected = 'boolean'; | ||
const obj = { | ||
arrayVal: [1, 2, 3], | ||
'boolean': false, | ||
'string': 'test', | ||
'innerObj': { | ||
innerKey: 'innerVal' | ||
}, | ||
'nullVal': null, | ||
'notDefined': undefined, | ||
'notANumber': NaN, | ||
'dateVal': new Date('2016-01-01'), | ||
'secondBool': false | ||
} | ||
let actual = osm.findKeyByValue(false, obj); | ||
expect(actual).to.eql(expected); | ||
const curried = osm.findKeyByValue(false); | ||
expect(curried(obj)).to.eql(expected); | ||
actual = osm.findKeyByValue(true, obj); | ||
expect(actual).to.eql(undefined); | ||
}); | ||
it('should return the first key that matches the value of a string', () => { | ||
const expected = 'string'; | ||
const obj = { | ||
arrayVal: [1, 2, 3], | ||
'boolean': false, | ||
'string': 'test', | ||
'innerObj': { | ||
innerKey: 'innerVal' | ||
}, | ||
'nullVal': null, | ||
'notDefined': undefined, | ||
'notANumber': NaN, | ||
'dateVal': new Date('2016-01-01') | ||
} | ||
let actual = osm.findKeyByValue('test', obj); | ||
expect(actual).to.eql(expected); | ||
actual = osm.findKeyByValue('', obj); | ||
expect(actual).to.eql(undefined); | ||
const curried = osm.findKeyByValue('test'); | ||
expect(curried(obj)).to.eql(expected); | ||
}); | ||
it('should return the first key that matches the null value', () => { | ||
const expected = 'nullVal'; | ||
const obj = { | ||
arrayVal: [1, 2, 3], | ||
'boolean': false, | ||
'string': 'test', | ||
'innerObj': { | ||
innerKey: 'innerVal' | ||
}, | ||
'nullVal': null, | ||
'notDefined': undefined, | ||
'notANumber': NaN, | ||
'dateVal': new Date('2016-01-01') | ||
} | ||
const actual = osm.findKeyByValue(null, obj); | ||
expect(actual).to.eql(expected); | ||
const curried = osm.findKeyByValue(null); | ||
expect(curried(obj)).to.eql(expected); | ||
}); | ||
it('should return the first key that matches the undefined value', () => { | ||
const expected = 'notDefined'; | ||
const obj = { | ||
arrayVal: [1, 2, 3], | ||
'boolean': false, | ||
'string': 'test', | ||
'innerObj': { | ||
innerKey: 'innerVal' | ||
}, | ||
'nullVal': null, | ||
'notDefined': undefined, | ||
'notANumber': NaN, | ||
'dateVal': new Date('2016-01-01') | ||
} | ||
const actual = osm.findKeyByValue(undefined, obj); | ||
expect(actual).to.eql(expected); | ||
const curried = osm.findKeyByValue(undefined); | ||
expect(curried(obj)).to.eql(expected); | ||
}); | ||
it('should return the false when searching for NaN', () => { | ||
const expected = 'notANumber'; | ||
const obj = { | ||
arrayVal: [1, 2, 3], | ||
'boolean': false, | ||
'string': 'test', | ||
'innerObj': { | ||
innerKey: 'innerVal' | ||
}, | ||
'nullVal': null, | ||
'notDefined': undefined, | ||
'notANumber': NaN, | ||
'dateVal': new Date('2016-01-01') | ||
} | ||
const actual = osm.findKeyByValue(NaN, obj); | ||
expect(actual).to.not.eql(expected); | ||
const curried = osm.findKeyByValue(NaN); | ||
expect(curried(obj)).to.not.eql(expected); | ||
}); | ||
it('should return the first key that matches the value of a date', () => { | ||
const expectedDate = new Date('2016-01-01') | ||
const expected = 'dateVal'; | ||
const obj = { | ||
arrayVal: [1, 2, 3], | ||
'boolean': false, | ||
'string': 'test', | ||
'innerObj': { | ||
innerKey: 'innerVal' | ||
}, | ||
'nullVal': null, | ||
'notDefined': undefined, | ||
'notANumber': NaN, | ||
'dateVal': new Date('2016-01-01') | ||
} | ||
const actual = osm.findKeyByValue(expectedDate, obj); | ||
expect(actual).to.eql(expected); | ||
const second = osm.findKeyByValue(new Date('2015-01-01'), obj); | ||
expect(second).to.eql(undefined); | ||
const curried = osm.findKeyByValue(expectedDate); | ||
expect(curried(obj)).to.eql(expected); | ||
}); | ||
//This behavior will be changed in a future version. For now, when the value is an object, return undefined | ||
it('should return the first key that matches the value of an object', () => { | ||
const comparatorObj = { | ||
innerKey: 'innerVal', | ||
test: 'best' | ||
}; | ||
const expected = undefined; | ||
const obj = { | ||
arrayVal: [1, 2, 3], | ||
'boolean': false, | ||
'string': 'test', | ||
'innerObj': { | ||
innerKey: 'innerVal' | ||
}, | ||
'nullVal': null, | ||
'notDefined': undefined, | ||
'notANumber': NaN, | ||
'dateVal': new Date('2016-01-01') | ||
} | ||
const actual = osm.findKeyByValue(comparatorObj, obj); | ||
expect(actual).to.eql(expected); | ||
const curried = osm.findKeyByValue(comparatorObj); | ||
expect(curried(obj)).to.eql(expected); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
54846
659
156