- Notifications
You must be signed in to change notification settings - Fork0
Functions to test/check, filter, find and process/transform objects
License
gamtiq/teo
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Functions to test/check, filter, find and process/transform objects.
npm install teo
component install gamtiq/teo
jam install teo
bower install teo
jspm install teo
spm install teo
Usedist/teo.js
ordist/teo.min.js
(minified version).
varteo=require("teo");
varteo=require("gamtiq/teo");...
require(["teo"],function(teo){ ...});
System.import("teo").then(function(teo){ ...});
define(["path/to/dist/teo.js"],function(teo){ ...});
<!-- Use bower_components/teo/dist/teo.js if the library was installed by Bower --><scripttype="text/javascript"src="path/to/dist/teo.js"></script><scripttype="text/javascript">// teo is available via teo field of window object ...</script>
teo.isObject(teo);// trueteo.isObject(null);// falseteo.isObject([]);// falseteo.isEmpty(teo);// falseteo.isEmpty([],true);// truevarobj={};obj[Symbol("a")]=null;teo.isEmpty(obj);// falseteo.isEmpty(obj,true);// trueteo.test({},"true");// trueteo.test({},"empty");// trueteo.test([],"empty");// trueteo.test(teo,"empty");// falseteo.test([0],"empty");// falseteo.test({},{});// trueteo.test({a:1},{a:2});// falseteo.test({a:1,b:2,c:3,d:4},{c:3,a:1});// trueteo.test(1,teo.isObject);// falseteo.test("",false);// truevarpersonList=[{name:"Adam",age:27,married:true,children:1},{name:"Eva",age:23,married:true,children:1},{name:"Carl",age:59,married:true,children:3},{name:"Daniel",age:17,married:false,children:0},{name:"Gloria",age:28,married:false,children:1},{name:"Viola",age:35,married:true,children:4},{name:"Leonardo",age:61,married:false,children:1},{name:"Patricia",age:44,married:false,children:2}];teo.filterList([0,"",true,"ret"],"true");// [true, "ret"]teo.filterList([3,-4,2,10,7,-9,5],function(n){returnn>=3;},{count:true});// 4teo.filterList(personList,function(person){returnperson.age>30;},{transform:function(person){returnperson.name;}});// ["Carl", "Viola", "Leonardo", "Patricia"]teo.findItemIndex(personList,{married:false,children:1});// 4teo.findItem(personList,function(person){returnperson.age>30&&!person.married;});// {name: "Leonardo", age: 61, married: false, children: 1}teo.map({a:1,b:2,c:null,d:"delta",e:null,f:undefined},function(context){returnfalse;},{filter:{value:null}});// {a: 1, b: 2, c: false, d: "delta", e: false, f: undefined}teo.map({a5:1,b3:"center",c5:null,d:"delta",e8:-5,field9:99,g99:-38},null,{rename:function(context){varsField=context.field,match=sField.match(/^\w\d+$/);returntypeofcontext.value==="number"&&match ?"n_"+match[0] :sField;}});// {n_a5: 1, b3: "center", c5: null, d: "delta", n_e8: -5, field9: 99, n_g99: -38}functionconvert(context){varvalue=context.value,bNoValue=value==null,sType=typeofvalue;if(context.test){returnbNoValue||sType==="object"||(sType==="string"&&/^-?\d+$/.test(value));}else{returnbNoValue ?0 :Number(value);}}teo.map({a:"abc",b:"25",c:{d:null,e:"eclipse",f:{g:undefined,h:"-59",i:"JS 2015"}}},convert,{filter:convert,recursion:true});// returns// {// a: "abc",// b: 25,// c: {// d: 0,// e: "eclipse",// f: {// g: 0,// h: -59,// i: "JS 2015"// }// }// }
See tests for additional examples.
Check whether value is real object (not array nor function).
Check whether object do not contain any fields.
Check whether object conforms to specified condition/filter.
filterList(list: Array, filter: Object | Function | Array | String, [settings: Object]): Array | Integer
Form new array containing elements from the source array which conform to the given condition (filter)or calculate quantity of such elements.
Return the index of the first element in the array that conforms to the given condition (filter).
Return the first element in the array that conforms to the given condition (filter).
Execute the specified action for fields of the object and return the object containing results of processing.
Seedoc
folder for details.
In lieu of a formal styleguide, take care to maintain the existing coding style.Add unit tests for any new or changed functionality.Lint and test your code usingGrunt.
Copyright (c) 2014-2015 Denis Sikuler
Licensed under the MIT license.
About
Functions to test/check, filter, find and process/transform objects