Comparing version 0.1.2 to 0.2.0
@@ -7,3 +7,3 @@ { | ||
], | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/gamtiq/adam", | ||
@@ -18,7 +18,17 @@ "authors": [ | ||
"create", | ||
"check", | ||
"field", | ||
"value", | ||
"type", | ||
"kind", | ||
"size", | ||
"split", | ||
"filter" | ||
"filter", | ||
"copy", | ||
"change", | ||
"map", | ||
"remove", | ||
"reverse", | ||
"empty", | ||
"transform" | ||
], | ||
@@ -25,0 +35,0 @@ "license": "MIT", |
@@ -5,3 +5,3 @@ { | ||
"description": "Functions to create, process and test objects", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"keywords": [ | ||
@@ -11,7 +11,17 @@ "object", | ||
"create", | ||
"check", | ||
"field", | ||
"value", | ||
"type", | ||
"kind", | ||
"size", | ||
"split", | ||
"filter" | ||
"filter", | ||
"copy", | ||
"change", | ||
"map", | ||
"remove", | ||
"reverse", | ||
"empty", | ||
"transform" | ||
], | ||
@@ -18,0 +28,0 @@ "dependencies": {}, |
320
dist/adam.js
@@ -136,3 +136,3 @@ | ||
/** | ||
* Check whether given value is an empty value i.e. `null`, `undefined`, empty object, empty array or empty string. | ||
* Check whether given value is an empty value i.e. `null`, `undefined`, `0`, empty object, empty array or empty string. | ||
* | ||
@@ -150,2 +150,3 @@ * @param {Any} value | ||
return value == null | ||
|| value === 0 | ||
|| value === "" | ||
@@ -496,2 +497,3 @@ || (typeof value === "object" && ! isSizeMore(value, 0)) | ||
* @alias module:adam.fromArray | ||
* @see {@link module:adam.checkField checkField} | ||
*/ | ||
@@ -557,2 +559,3 @@ function fromArray(list, keyField, settings) { | ||
* @alias module:adam.split | ||
* @see {@link module:adam.checkField checkField} | ||
*/ | ||
@@ -586,6 +589,315 @@ function split(obj, firstObjFields, settings) { | ||
/** | ||
* Remove filtered fields/elements from specified object/array. | ||
* | ||
* @param {Array | Object} obj | ||
* Array or object to be processed. | ||
* @param {Any} filter | ||
* A filter or array of filters specifying fields or elements that should be removed | ||
* (see {@link module:adam.checkField checkField}). | ||
* @param {Object} [settings] | ||
* Operation settings. Keys are settings names, values are corresponding settings values. | ||
* The following settings are supported: | ||
* | ||
* * `filterConnect`: `String` - a boolean connector that should be used when array of filters is specified | ||
* in `filter` parameter (see {@link module:adam.checkField checkField}) | ||
* @return {Array | Object} | ||
* Processed array or object. | ||
* @alias module:adam.remove | ||
* @see {@link module:adam.checkField checkField} | ||
*/ | ||
function remove(obj, filter, settings) { | ||
var field; | ||
if (obj && typeof obj === "object") { | ||
if (getClass(obj) === "Array") { | ||
field = obj.length; | ||
while(field--) { | ||
if (checkField(obj, field, filter, settings)) { | ||
obj.splice(field, 1); | ||
} | ||
} | ||
} | ||
else { | ||
for (field in obj) { | ||
if (checkField(obj, field, filter, settings)) { | ||
delete obj[field]; | ||
} | ||
} | ||
} | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Empty the given value according to the following rules: | ||
* | ||
* * for array: removes all elements from the value | ||
* * for object: removes all own fields from the value | ||
* * for string: returns empty string | ||
* * for number: returns `0` | ||
* * otherwise: returns `undefined` | ||
* | ||
* @param {Any} value | ||
* Value to be processed. | ||
* @return {Any} | ||
* Processed value (for array or object) or empty value corresponding to the given value. | ||
* @alias module:adam.empty | ||
*/ | ||
function empty(value) { | ||
var sField; | ||
switch (getType(value)) { | ||
case "object": | ||
if (getClass(value) === "Array") { | ||
value.length = 0; | ||
} | ||
else { | ||
for (sField in value) { | ||
delete value[sField]; | ||
} | ||
} | ||
break; | ||
case "string": | ||
value = ""; | ||
break; | ||
case "number": | ||
value = 0; | ||
break; | ||
default: | ||
value = sField; | ||
} | ||
return value; | ||
} | ||
/** | ||
* Reverse or negate the given value according to the following rules: | ||
* | ||
* * for array: reverses order of its elements | ||
* * for object: swaps fields with their values (i.e. for `{a: "b", c: "d"}` returns `{b: "a", d: "c"}`) | ||
* * for string: reverses order of its characters | ||
* * for number: returns negated value (i.e. `- value`) | ||
* * for boolean: returns negated value (i.e. `! value`) | ||
* * otherwise: returns source value without modification | ||
* | ||
* @param {Any} value | ||
* Value to be processed. | ||
* @return {Any} | ||
* Processed value. | ||
* @alias module:adam.reverse | ||
*/ | ||
function reverse(value) { | ||
var cache, sField; | ||
switch (getType(value)) { | ||
case "object": | ||
if (getClass(value) === "Array") { | ||
value = value.reverse(); | ||
} | ||
else { | ||
cache = {}; | ||
for (sField in value) { | ||
cache[ value[sField] ] = sField; | ||
} | ||
value = cache; | ||
} | ||
break; | ||
case "string": | ||
value = value.split("").reverse().join(""); | ||
break; | ||
case "number": | ||
value = - value; | ||
break; | ||
case "boolean": | ||
value = ! value; | ||
break; | ||
} | ||
return value; | ||
} | ||
/** | ||
* Transform the given value applying the specified operation. | ||
* | ||
* @param {Any} value | ||
* Value to be transformed. | ||
* @param {String} sAction | ||
* Operation that should be applied to transform the value. Can be one of the following: | ||
* | ||
* * `array` - convert the value to array (using `Array(value)`) | ||
* * `boolean` - convert the value to boolean value (using `Boolean(value)`) | ||
* * `empty` - empty the value (see {@link module:adam.empty empty}) | ||
* * `function` - convert the value to function (using `Function(value)`) | ||
* * `integer` - try to convert the value to an integer number (using `Math.round(Number(value)`) | ||
* * `number` - try to convert the value to number (using `Number(value)`) | ||
* * `object` - convert the value to object (using `Object(value)`) | ||
* * `reverse` - reverse the value (see {@link module:adam.reverse reverse}) | ||
* * `string` - convert the value to string (using `String(value)`) | ||
* * otherwise - source value | ||
* @return {Any} | ||
* Transformed value. | ||
* @alias module:adam.transform | ||
* @see {@link module:adam.empty empty} | ||
* @see {@link module:adam.reverse reverse} | ||
*/ | ||
function transform(value, sAction) { | ||
/*jshint evil:true, -W064*/ | ||
var result; | ||
switch (sAction) { | ||
case "array": | ||
result = Array(value); | ||
break; | ||
case "boolean": | ||
result = Boolean(value); | ||
break; | ||
case "empty": | ||
result = empty(value); | ||
break; | ||
case "function": | ||
result = Function(value); | ||
break; | ||
case "integer": | ||
result = Math.round(Number(value)); | ||
break; | ||
case "number": | ||
result = Number(value); | ||
break; | ||
case "object": | ||
result = Object(value); | ||
break; | ||
case "reverse": | ||
result = reverse(value); | ||
break; | ||
case "string": | ||
result = String(value); | ||
break; | ||
default: | ||
result = value; | ||
} | ||
return result; | ||
} | ||
/** | ||
* Copy all or filtered fields from source object into target object, applying specified transformation if necessary. | ||
* | ||
* @param {Object} source | ||
* Object whose fields will be copied. | ||
* @param {Object} target | ||
* Object into which fields should be copied. | ||
* @param {Object} [settings] | ||
* Operation settings. Keys are settings names, values are corresponding settings values. | ||
* The following settings are supported: | ||
* | ||
* * `filter` - a filter that should be used to select fields for copying (see {@link module:adam.checkField checkField}); | ||
* fields conforming to the filter will be copied | ||
* * `filterConnect`: `String` - a boolean connector that should be used when array of filters is specified | ||
* in `filter` setting (see {@link module:adam.checkField checkField}) | ||
* * `transform` - an action/operation that should be applied to get field's value that will be copied | ||
* instead of value from source object; can be a string specifying transformation (see {@link module:adam.transform transform}) | ||
* or a function whose result will be used as field's value; object with the following fields will be passed into the function: | ||
* - `field` - field name | ||
* - `value` field value from source object | ||
* - `source` - reference to the source object | ||
* - `target` - reference to the target object | ||
* @return {Object} | ||
* Reference to the target object (value of `target` parameter). | ||
* @alias module:adam.copy | ||
* @see {@link module:adam.checkField checkField} | ||
* @see {@link module:adam.transform transform} | ||
*/ | ||
function copy(source, target, settings) { | ||
/*jshint laxbreak:true*/ | ||
var bAll = true, | ||
bFuncAction, action, filter, key; | ||
if (! settings) { | ||
settings = {}; | ||
} | ||
if ("filter" in settings) { | ||
filter = settings.filter; | ||
bAll = false; | ||
} | ||
action = settings.transform; | ||
if (typeof action === "function") { | ||
bFuncAction = true; | ||
} | ||
for (key in source) { | ||
if (bAll || checkField(source, key, filter, settings)) { | ||
target[key] = action | ||
? (bFuncAction | ||
? action({source: source, target: target, field: key, value: source[key]}) | ||
: transform(source[key], action)) | ||
: source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
/** | ||
* Change all or filtered fields of object, applying specified action/transformation. | ||
* | ||
* @param {Object} obj | ||
* Object whose fields should be changed. | ||
* @param {Function | String} action | ||
* An action/operation that should be applied to get new field value. | ||
* See description of `transform` setting of {@link module:adam.copy copy}. | ||
* @param {Object} [settings] | ||
* Operation settings. Keys are settings names, values are corresponding settings values. | ||
* The following settings are supported: | ||
* | ||
* * `filter` - a filter that should be used to select fields for modification (see {@link module:adam.checkField checkField}); | ||
* fields conforming to the filter will be changed | ||
* * `filterConnect`: `String` - a boolean connector that should be used when array of filters is specified | ||
* in `filter` setting (see {@link module:adam.checkField checkField}) | ||
* @return {Object} | ||
* Modified object (value of `obj` parameter). | ||
* @alias module:adam.change | ||
* @see {@link module:adam.checkField checkField} | ||
* @see {@link module:adam.copy copy} | ||
*/ | ||
function change(obj, action, settings) { | ||
/*jshint laxbreak:true*/ | ||
settings = settings | ||
? copy(settings, {}) | ||
: {}; | ||
settings.transform = action; | ||
return copy(obj, obj, settings); | ||
} | ||
/** | ||
* Create new object containing all or filtered fields of the source object/array, | ||
* applying specified action/transformation for field values. | ||
* | ||
* @param {Array | Object} obj | ||
* Array/object whose fields should be copied. | ||
* @param {Function | String} action | ||
* An action/operation that should be applied to get field value that will be saved in created object. | ||
* See description of `transform` setting of {@link module:adam.copy copy}. | ||
* @param {Object} [settings] | ||
* Operation settings. Keys are settings names, values are corresponding settings values. | ||
* The following settings are supported: | ||
* | ||
* * `filter` - a filter that should be used to select fields for copying (see {@link module:adam.checkField checkField}); | ||
* fields conforming to the filter will be copied in created object | ||
* * `filterConnect`: `String` - a boolean connector that should be used when array of filters is specified | ||
* in `filter` setting (see {@link module:adam.checkField checkField}) | ||
* @return {Object} | ||
* Created object containing processed fields. | ||
* @alias module:adam.map | ||
* @see {@link module:adam.checkField checkField} | ||
* @see {@link module:adam.copy copy} | ||
*/ | ||
function map(obj, action, settings) { | ||
/*jshint laxbreak:true*/ | ||
settings = settings | ||
? copy(settings, {}) | ||
: {}; | ||
settings.transform = action; | ||
return copy(obj, | ||
getClass(obj) === "Array" ? [] : {}, | ||
settings); | ||
} | ||
// Exports | ||
module.exports = { | ||
change: change, | ||
checkField: checkField, | ||
copy: copy, | ||
empty: empty, | ||
fromArray: fromArray, | ||
@@ -602,3 +914,7 @@ getClass: getClass, | ||
isSizeMore: isSizeMore, | ||
split: split | ||
map: map, | ||
remove: remove, | ||
reverse: reverse, | ||
split: split, | ||
transform: transform | ||
}; | ||
@@ -605,0 +921,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(a,b){if("object"==typeof exports)module.exports=b(require,exports,module);else if("function"==typeof define&&define.amd)define(["require","exports","module"],b);else{var c=function(b){return a[b]},d=a,e={exports:d};a.adam=b(c,d,e)}}(this,function(a,b,c){"use strict";function d(a){var b=Object.prototype.toString.call(a);return b.substring(8,b.length-1)}function e(a){var b=typeof a;return null===a?"null":"number"===b&&isNaN(a)?"nan":b}function f(a,b){var c,d=0,e=!(b&&"filter"in b),f=e?null:b.filter;for(c in a)(e||j(a,c,f,b))&&d++;return d}function g(a,b,c){var d,e=0,f=!(c&&"filter"in c),g=f?null:c.filter;if(b>=0)for(d in a)if((f||j(a,d,g,c))&&++e>b)return!0;return e>b}function h(a){return null==a||""===a||"object"==typeof a&&!g(a,0)||"Array"===d(a)&&0===a.length}function i(a,b){var c,f,g,i="!"===b.charAt(0),j=e(a);return i&&(b=b.substring(1)),g=j===b||d(a)===b||"true"===b&&Boolean(a)||"false"===b&&!a||"empty"===b&&h(a)||"number"===j&&("zero"===b&&0===a||"positive"===b&&a>0||"negative"===b&&0>a||(c=a===Number.POSITIVE_INFINITY||a===Number.NEGATIVE_INFINITY)&&"infinity"===b||!c&&((f=a===Math.ceil(a))&&"integer"===b||f&&"even"===b&&a%2===0||f&&"odd"===b&&a%2!==0||!f&&"real"===b)),i?!g:g}function j(a,b,c,e){var f,g,h,k,l=!0;b=String(b),e||(e={}),k="value"in e?e.value:a[b],f=e.filterConnect,f="string"!=typeof f||"or"!==f.toLowerCase(),"Array"!==d(c)&&(c=[c]);for(g=0,h=c.length;h>g;g++){switch(l=c[g],d(l)){case"Function":l=Boolean(l(k,b,a));break;case"String":l="own"===l?a.hasOwnProperty(b):"!own"===l?!a.hasOwnProperty(b):i(k,l);break;case"RegExp":l=l.test(k);break;case"Object":"and"in l?l=j(a,b,l.and,{filterConnect:"and"}):"or"in l?l=j(a,b,l.or,{filterConnect:"or"}):"field"in l?(l=l.field,l="RegExp"===d(l)?l.test(b):String(l)===b):l="value"in l?l.value===k:l===k;break;default:l=l===k}if(f&&!l||!f&&l)break}return l}function k(a,b){var c,d=[],e=!(b&&"filter"in b),f=e?null:b.filter;for(c in a)(e||j(a,c,f,b))&&d.push(c);return d}function l(a,b,c){var d,e=[];for(d in a)if(a[d]===b){if(!c)return d;e.push(d)}return e.length?e:null}function m(a,b){var c,d=[],e=!(b&&"filter"in b),f=e?null:b.filter;for(c in a)(e||j(a,c,f,b))&&(d[d.length]=a[c]);return d}function n(a,b){var c,d,e,f;if(b||(b={}),f=b.prefix,d=b.startNum,c=b.checkPrefix,"string"!=typeof f&&(f="f"),d||(d=0),c?(e=f,d--):e=f+d,a)for(;e in a;)e=f+ ++d;return e}function o(a,b,c){var d,e,f,g,h,i,k,l,m,n=a.length,o={};if(n){c||(c={}),d=!("filter"in c),g=d?null:c.filter,h=c.filterConnect,f="function"==typeof b,e=Boolean(c.deleteKeyField&&b),f||(m=b);for(l=0;n>l;l++)i=a[l],f?k=m=b(i,o,l):(k=m?i[m]:i,"function"==typeof k&&(k=k.call(i))),(d||j(o,k,g,{value:i,filterConnect:h}))&&(e&&delete i[m],o[k]=i)}return o}function p(a,b,c){var d,e,f,g={},h={},i=[g,h];c||(c={}),b?(d=!0,"number"==typeof b.length&&(b=o(b))):(d=!1,e=c.filter);for(f in a)((d?f in b:j(a,f,e,c))?g:h)[f]=a[f];return i}return c.exports={checkField:j,fromArray:o,getClass:d,getFields:k,getFreeField:n,getSize:f,getType:e,getValueKey:l,getValues:m,isEmpty:h,isKindOf:i,isSizeMore:g,split:p},c.exports}); | ||
!function(a,b){if("object"==typeof exports)module.exports=b(require,exports,module);else if("function"==typeof define&&define.amd)define(["require","exports","module"],b);else{var c=function(b){return a[b]},d=a,e={exports:d};a.adam=b(c,d,e)}}(this,function(a,b,c){"use strict";function d(a){var b=Object.prototype.toString.call(a);return b.substring(8,b.length-1)}function e(a){var b=typeof a;return null===a?"null":"number"===b&&isNaN(a)?"nan":b}function f(a,b){var c,d=0,e=!(b&&"filter"in b),f=e?null:b.filter;for(c in a)(e||j(a,c,f,b))&&d++;return d}function g(a,b,c){var d,e=0,f=!(c&&"filter"in c),g=f?null:c.filter;if(b>=0)for(d in a)if((f||j(a,d,g,c))&&++e>b)return!0;return e>b}function h(a){return null==a||0===a||""===a||"object"==typeof a&&!g(a,0)||"Array"===d(a)&&0===a.length}function i(a,b){var c,f,g,i="!"===b.charAt(0),j=e(a);return i&&(b=b.substring(1)),g=j===b||d(a)===b||"true"===b&&Boolean(a)||"false"===b&&!a||"empty"===b&&h(a)||"number"===j&&("zero"===b&&0===a||"positive"===b&&a>0||"negative"===b&&0>a||(c=a===Number.POSITIVE_INFINITY||a===Number.NEGATIVE_INFINITY)&&"infinity"===b||!c&&((f=a===Math.ceil(a))&&"integer"===b||f&&"even"===b&&a%2===0||f&&"odd"===b&&a%2!==0||!f&&"real"===b)),i?!g:g}function j(a,b,c,e){var f,g,h,k,l=!0;b=String(b),e||(e={}),k="value"in e?e.value:a[b],f=e.filterConnect,f="string"!=typeof f||"or"!==f.toLowerCase(),"Array"!==d(c)&&(c=[c]);for(g=0,h=c.length;h>g;g++){switch(l=c[g],d(l)){case"Function":l=Boolean(l(k,b,a));break;case"String":l="own"===l?a.hasOwnProperty(b):"!own"===l?!a.hasOwnProperty(b):i(k,l);break;case"RegExp":l=l.test(k);break;case"Object":"and"in l?l=j(a,b,l.and,{filterConnect:"and"}):"or"in l?l=j(a,b,l.or,{filterConnect:"or"}):"field"in l?(l=l.field,l="RegExp"===d(l)?l.test(b):String(l)===b):l="value"in l?l.value===k:l===k;break;default:l=l===k}if(f&&!l||!f&&l)break}return l}function k(a,b){var c,d=[],e=!(b&&"filter"in b),f=e?null:b.filter;for(c in a)(e||j(a,c,f,b))&&d.push(c);return d}function l(a,b,c){var d,e=[];for(d in a)if(a[d]===b){if(!c)return d;e.push(d)}return e.length?e:null}function m(a,b){var c,d=[],e=!(b&&"filter"in b),f=e?null:b.filter;for(c in a)(e||j(a,c,f,b))&&(d[d.length]=a[c]);return d}function n(a,b){var c,d,e,f;if(b||(b={}),f=b.prefix,d=b.startNum,c=b.checkPrefix,"string"!=typeof f&&(f="f"),d||(d=0),c?(e=f,d--):e=f+d,a)for(;e in a;)e=f+ ++d;return e}function o(a,b,c){var d,e,f,g,h,i,k,l,m,n=a.length,o={};if(n){c||(c={}),d=!("filter"in c),g=d?null:c.filter,h=c.filterConnect,f="function"==typeof b,e=Boolean(c.deleteKeyField&&b),f||(m=b);for(l=0;n>l;l++)i=a[l],f?k=m=b(i,o,l):(k=m?i[m]:i,"function"==typeof k&&(k=k.call(i))),(d||j(o,k,g,{value:i,filterConnect:h}))&&(e&&delete i[m],o[k]=i)}return o}function p(a,b,c){var d,e,f,g={},h={},i=[g,h];c||(c={}),b?(d=!0,"number"==typeof b.length&&(b=o(b))):(d=!1,e=c.filter);for(f in a)((d?f in b:j(a,f,e,c))?g:h)[f]=a[f];return i}function q(a,b,c){var e;if(a&&"object"==typeof a)if("Array"===d(a))for(e=a.length;e--;)j(a,e,b,c)&&a.splice(e,1);else for(e in a)j(a,e,b,c)&&delete a[e];return a}function r(a){var b;switch(e(a)){case"object":if("Array"===d(a))a.length=0;else for(b in a)delete a[b];break;case"string":a="";break;case"number":a=0;break;default:a=b}return a}function s(a){var b,c;switch(e(a)){case"object":if("Array"===d(a))a=a.reverse();else{b={};for(c in a)b[a[c]]=c;a=b}break;case"string":a=a.split("").reverse().join("");break;case"number":a=-a;break;case"boolean":a=!a}return a}function t(a,b){var c;switch(b){case"array":c=Array(a);break;case"boolean":c=Boolean(a);break;case"empty":c=r(a);break;case"function":c=Function(a);break;case"integer":c=Math.round(Number(a));break;case"number":c=Number(a);break;case"object":c=Object(a);break;case"reverse":c=s(a);break;case"string":c=String(a);break;default:c=a}return c}function u(a,b,c){var d,e,f,g,h=!0;c||(c={}),"filter"in c&&(f=c.filter,h=!1),e=c.transform,"function"==typeof e&&(d=!0);for(g in a)(h||j(a,g,f,c))&&(b[g]=e?d?e({source:a,target:b,field:g,value:a[g]}):t(a[g],e):a[g]);return b}function v(a,b,c){return c=c?u(c,{}):{},c.transform=b,u(a,a,c)}function w(a,b,c){return c=c?u(c,{}):{},c.transform=b,u(a,"Array"===d(a)?[]:{},c)}return c.exports={change:v,checkField:j,copy:u,empty:r,fromArray:o,getClass:d,getFields:k,getFreeField:n,getSize:f,getType:e,getValueKey:l,getValues:m,isEmpty:h,isKindOf:i,isSizeMore:g,map:w,remove:q,reverse:s,split:p,transform:t},c.exports}); |
@@ -0,1 +1,6 @@ | ||
### 0.2.0 / 2014-10-22 | ||
* add methods `remove`, `empty`, `reverse`, `transform`, `copy`, `change`, `map` | ||
* `isEmpty` treats `0` as empty value | ||
### 0.1.2 / 2014-10-12 | ||
@@ -2,0 +7,0 @@ |
{ | ||
"name": "adam", | ||
"description": "Functions to create, process and test objects", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/gamtiq/adam", | ||
@@ -44,7 +44,17 @@ "author": { | ||
"create", | ||
"check", | ||
"field", | ||
"value", | ||
"type", | ||
"kind", | ||
"size", | ||
"split", | ||
"filter" | ||
"filter", | ||
"copy", | ||
"change", | ||
"map", | ||
"remove", | ||
"reverse", | ||
"empty", | ||
"transform" | ||
], | ||
@@ -51,0 +61,0 @@ "jam": { |
@@ -76,2 +76,6 @@ # adam | ||
```js | ||
function inc(data) { | ||
return ++data.value; | ||
} | ||
var obj = {a: 1, b: 2, c: 3, d: 4, e: 5}; | ||
@@ -95,4 +99,4 @@ | ||
adam.getFields(obj); // ["a", "b", "c", "d", "e"] | ||
adam.getFields(obj, {filter: function(value) {return value > 3;}}); // ["d", "e"] | ||
adam.getFields(adam, {filter: {field: /^is/}}); // ["isEmpty", "isKindOf", "isSizeMore"] | ||
adam.getFields(obj, {filter: function(value) {return value < 4;}}); // ["a", "b", "c"] | ||
adam.getFields(obj, {filter: {field: /^[d-h]/}}); // ["d", "e"] | ||
@@ -108,2 +112,23 @@ adam.getValues(obj); // [1, 2, 3, 4, 5] | ||
adam.split(obj, null, {filter: ["even", /3/], filterConnect: "or"}); // [{b: 2, c: 3, d: 4}, {a: 1, e: 5}] | ||
adam.remove({a: 1, b: "2", c: 3}, "string"); // {a: 1, c: 3} | ||
adam.remove([1, 2, 3, 4, 5], "even"); // [1, 3, 5] | ||
adam.empty({x: -1, y: 9}); // {} | ||
adam.reverse({a: "x", b: "files"}); // {x: "a", files: "b"} | ||
adam.reverse("eval"); // "lave" | ||
adam.transform("7.381", "string"); // 7 | ||
adam.copy(obj, {b: "no", z: "a"}); // {a: 1, b: 2, c: 3, d: 4, e: 5, z: "a"} | ||
adam.copy(obj, {b: "no", z: "a"}, {filter: "odd"}); // {a: 1, b: "no", c: 3, e: 5, z: "a"} | ||
adam.copy(obj, {b: "no", z: "a"}, {filter: "even", transform: inc}); // {b: 3, d: 5, z: "a"} | ||
adam.change({a: 1, b: 2, c: 3}, "reverse"); // {a: -1, b: -2, c: -3} | ||
adam.change({a: 10, b: 28, c: -3, d: null, e: "zero = 0"}, "empty", {filter: /0/}); // {a: 0, b: 28, c: -3, d: null, e: ""} | ||
adam.change([1, 2, 3, 4, 5], "reverse", {filter: "even"}); // [1, -2, 3, -4, 5] | ||
adam.map(obj, "reverse", {filter: "odd"}); // {a: -1, c: -3, e: -5} | ||
adam.map(["1", "2", "3"], "number"); // [1, 2, 3] | ||
``` | ||
@@ -115,2 +140,6 @@ | ||
### change(obj: Object, action: Function | String, [settings: Object]): Object | ||
Change all or filtered fields of object, applying specified action/transformation. | ||
### checkField(obj: Object, field: String, filter: Any, [settings: Object]): Boolean | ||
@@ -120,2 +149,10 @@ | ||
### copy(source: Object, target: Object, [settings: Object]): Object | ||
Copy all or filtered fields from source object into target object, applying specified transformation if necessary. | ||
### empty(value: Any): Any | ||
Empty the given value. | ||
### fromArray(list: Array, [keyField: Function | String], [settings: Object]): Object | ||
@@ -155,3 +192,3 @@ | ||
Check whether given value is an empty value i.e. `null`, `undefined`, empty object, empty array or empty string. | ||
Check whether given value is an empty value i.e. `null`, `undefined`, `0`, empty object, empty array or empty string. | ||
@@ -166,2 +203,15 @@ ### isKindOf(value: Any, kind: String): Boolean | ||
### map(obj: Object, action: Function | String, [settings: Object]): Object | ||
Create new object containing all or filtered fields of the source object/array, | ||
applying specified action/transformation for field values. | ||
### remove(obj: Array | Object, filter: Any, [settings: Object]): Array | Object | ||
Remove filtered fields/elements from specified object/array. | ||
### reverse(value: Any): Any | ||
Reverse or negate the given value. | ||
### split(obj: Object, firstObjFields: Array | Object | null, [settings: Object]): Array | ||
@@ -171,2 +221,6 @@ | ||
### transform(value: Any, action: String): Any | ||
Transform the given value applying the specified operation. | ||
See `doc` folder for details. | ||
@@ -173,0 +227,0 @@ |
318
src/adam.js
@@ -121,3 +121,3 @@ /* | ||
/** | ||
* Check whether given value is an empty value i.e. `null`, `undefined`, empty object, empty array or empty string. | ||
* Check whether given value is an empty value i.e. `null`, `undefined`, `0`, empty object, empty array or empty string. | ||
* | ||
@@ -135,2 +135,3 @@ * @param {Any} value | ||
return value == null | ||
|| value === 0 | ||
|| value === "" | ||
@@ -571,6 +572,315 @@ || (typeof value === "object" && ! isSizeMore(value, 0)) | ||
/** | ||
* Remove filtered fields/elements from specified object/array. | ||
* | ||
* @param {Array | Object} obj | ||
* Array or object to be processed. | ||
* @param {Any} filter | ||
* A filter or array of filters specifying fields or elements that should be removed | ||
* (see {@link module:adam.checkField checkField}). | ||
* @param {Object} [settings] | ||
* Operation settings. Keys are settings names, values are corresponding settings values. | ||
* The following settings are supported: | ||
* | ||
* * `filterConnect`: `String` - a boolean connector that should be used when array of filters is specified | ||
* in `filter` parameter (see {@link module:adam.checkField checkField}) | ||
* @return {Array | Object} | ||
* Processed array or object. | ||
* @alias module:adam.remove | ||
* @see {@link module:adam.checkField checkField} | ||
*/ | ||
function remove(obj, filter, settings) { | ||
var field; | ||
if (obj && typeof obj === "object") { | ||
if (getClass(obj) === "Array") { | ||
field = obj.length; | ||
while(field--) { | ||
if (checkField(obj, field, filter, settings)) { | ||
obj.splice(field, 1); | ||
} | ||
} | ||
} | ||
else { | ||
for (field in obj) { | ||
if (checkField(obj, field, filter, settings)) { | ||
delete obj[field]; | ||
} | ||
} | ||
} | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Empty the given value according to the following rules: | ||
* | ||
* * for array: removes all elements from the value | ||
* * for object: removes all own fields from the value | ||
* * for string: returns empty string | ||
* * for number: returns `0` | ||
* * otherwise: returns `undefined` | ||
* | ||
* @param {Any} value | ||
* Value to be processed. | ||
* @return {Any} | ||
* Processed value (for array or object) or empty value corresponding to the given value. | ||
* @alias module:adam.empty | ||
*/ | ||
function empty(value) { | ||
var sField; | ||
switch (getType(value)) { | ||
case "object": | ||
if (getClass(value) === "Array") { | ||
value.length = 0; | ||
} | ||
else { | ||
for (sField in value) { | ||
delete value[sField]; | ||
} | ||
} | ||
break; | ||
case "string": | ||
value = ""; | ||
break; | ||
case "number": | ||
value = 0; | ||
break; | ||
default: | ||
value = sField; | ||
} | ||
return value; | ||
} | ||
/** | ||
* Reverse or negate the given value according to the following rules: | ||
* | ||
* * for array: reverses order of its elements | ||
* * for object: swaps fields with their values (i.e. for `{a: "b", c: "d"}` returns `{b: "a", d: "c"}`) | ||
* * for string: reverses order of its characters | ||
* * for number: returns negated value (i.e. `- value`) | ||
* * for boolean: returns negated value (i.e. `! value`) | ||
* * otherwise: returns source value without modification | ||
* | ||
* @param {Any} value | ||
* Value to be processed. | ||
* @return {Any} | ||
* Processed value. | ||
* @alias module:adam.reverse | ||
*/ | ||
function reverse(value) { | ||
var cache, sField; | ||
switch (getType(value)) { | ||
case "object": | ||
if (getClass(value) === "Array") { | ||
value = value.reverse(); | ||
} | ||
else { | ||
cache = {}; | ||
for (sField in value) { | ||
cache[ value[sField] ] = sField; | ||
} | ||
value = cache; | ||
} | ||
break; | ||
case "string": | ||
value = value.split("").reverse().join(""); | ||
break; | ||
case "number": | ||
value = - value; | ||
break; | ||
case "boolean": | ||
value = ! value; | ||
break; | ||
} | ||
return value; | ||
} | ||
/** | ||
* Transform the given value applying the specified operation. | ||
* | ||
* @param {Any} value | ||
* Value to be transformed. | ||
* @param {String} sAction | ||
* Operation that should be applied to transform the value. Can be one of the following: | ||
* | ||
* * `array` - convert the value to array (using `Array(value)`) | ||
* * `boolean` - convert the value to boolean value (using `Boolean(value)`) | ||
* * `empty` - empty the value (see {@link module:adam.empty empty}) | ||
* * `function` - convert the value to function (using `Function(value)`) | ||
* * `integer` - try to convert the value to an integer number (using `Math.round(Number(value)`) | ||
* * `number` - try to convert the value to number (using `Number(value)`) | ||
* * `object` - convert the value to object (using `Object(value)`) | ||
* * `reverse` - reverse the value (see {@link module:adam.reverse reverse}) | ||
* * `string` - convert the value to string (using `String(value)`) | ||
* * otherwise - source value | ||
* @return {Any} | ||
* Transformed value. | ||
* @alias module:adam.transform | ||
* @see {@link module:adam.empty empty} | ||
* @see {@link module:adam.reverse reverse} | ||
*/ | ||
function transform(value, sAction) { | ||
/*jshint evil:true, -W064*/ | ||
var result; | ||
switch (sAction) { | ||
case "array": | ||
result = Array(value); | ||
break; | ||
case "boolean": | ||
result = Boolean(value); | ||
break; | ||
case "empty": | ||
result = empty(value); | ||
break; | ||
case "function": | ||
result = Function(value); | ||
break; | ||
case "integer": | ||
result = Math.round(Number(value)); | ||
break; | ||
case "number": | ||
result = Number(value); | ||
break; | ||
case "object": | ||
result = Object(value); | ||
break; | ||
case "reverse": | ||
result = reverse(value); | ||
break; | ||
case "string": | ||
result = String(value); | ||
break; | ||
default: | ||
result = value; | ||
} | ||
return result; | ||
} | ||
/** | ||
* Copy all or filtered fields from source object into target object, applying specified transformation if necessary. | ||
* | ||
* @param {Object} source | ||
* Object whose fields will be copied. | ||
* @param {Object} target | ||
* Object into which fields should be copied. | ||
* @param {Object} [settings] | ||
* Operation settings. Keys are settings names, values are corresponding settings values. | ||
* The following settings are supported: | ||
* | ||
* * `filter` - a filter that should be used to select fields for copying (see {@link module:adam.checkField checkField}); | ||
* fields conforming to the filter will be copied | ||
* * `filterConnect`: `String` - a boolean connector that should be used when array of filters is specified | ||
* in `filter` setting (see {@link module:adam.checkField checkField}) | ||
* * `transform` - an action/operation that should be applied to get field's value that will be copied | ||
* instead of value from source object; can be a string specifying transformation (see {@link module:adam.transform transform}) | ||
* or a function whose result will be used as field's value; object with the following fields will be passed into the function: | ||
* - `field` - field name | ||
* - `value` field value from source object | ||
* - `source` - reference to the source object | ||
* - `target` - reference to the target object | ||
* @return {Object} | ||
* Reference to the target object (value of `target` parameter). | ||
* @alias module:adam.copy | ||
* @see {@link module:adam.checkField checkField} | ||
* @see {@link module:adam.transform transform} | ||
*/ | ||
function copy(source, target, settings) { | ||
/*jshint laxbreak:true*/ | ||
var bAll = true, | ||
bFuncAction, action, filter, key; | ||
if (! settings) { | ||
settings = {}; | ||
} | ||
if ("filter" in settings) { | ||
filter = settings.filter; | ||
bAll = false; | ||
} | ||
action = settings.transform; | ||
if (typeof action === "function") { | ||
bFuncAction = true; | ||
} | ||
for (key in source) { | ||
if (bAll || checkField(source, key, filter, settings)) { | ||
target[key] = action | ||
? (bFuncAction | ||
? action({source: source, target: target, field: key, value: source[key]}) | ||
: transform(source[key], action)) | ||
: source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
/** | ||
* Change all or filtered fields of object, applying specified action/transformation. | ||
* | ||
* @param {Object} obj | ||
* Object whose fields should be changed. | ||
* @param {Function | String} action | ||
* An action/operation that should be applied to get new field value. | ||
* See description of `transform` setting of {@link module:adam.copy copy}. | ||
* @param {Object} [settings] | ||
* Operation settings. Keys are settings names, values are corresponding settings values. | ||
* The following settings are supported: | ||
* | ||
* * `filter` - a filter that should be used to select fields for modification (see {@link module:adam.checkField checkField}); | ||
* fields conforming to the filter will be changed | ||
* * `filterConnect`: `String` - a boolean connector that should be used when array of filters is specified | ||
* in `filter` setting (see {@link module:adam.checkField checkField}) | ||
* @return {Object} | ||
* Modified object (value of `obj` parameter). | ||
* @alias module:adam.change | ||
* @see {@link module:adam.checkField checkField} | ||
* @see {@link module:adam.copy copy} | ||
*/ | ||
function change(obj, action, settings) { | ||
/*jshint laxbreak:true*/ | ||
settings = settings | ||
? copy(settings, {}) | ||
: {}; | ||
settings.transform = action; | ||
return copy(obj, obj, settings); | ||
} | ||
/** | ||
* Create new object containing all or filtered fields of the source object/array, | ||
* applying specified action/transformation for field values. | ||
* | ||
* @param {Array | Object} obj | ||
* Array/object whose fields should be copied. | ||
* @param {Function | String} action | ||
* An action/operation that should be applied to get field value that will be saved in created object. | ||
* See description of `transform` setting of {@link module:adam.copy copy}. | ||
* @param {Object} [settings] | ||
* Operation settings. Keys are settings names, values are corresponding settings values. | ||
* The following settings are supported: | ||
* | ||
* * `filter` - a filter that should be used to select fields for copying (see {@link module:adam.checkField checkField}); | ||
* fields conforming to the filter will be copied in created object | ||
* * `filterConnect`: `String` - a boolean connector that should be used when array of filters is specified | ||
* in `filter` setting (see {@link module:adam.checkField checkField}) | ||
* @return {Object} | ||
* Created object containing processed fields. | ||
* @alias module:adam.map | ||
* @see {@link module:adam.checkField checkField} | ||
* @see {@link module:adam.copy copy} | ||
*/ | ||
function map(obj, action, settings) { | ||
/*jshint laxbreak:true*/ | ||
settings = settings | ||
? copy(settings, {}) | ||
: {}; | ||
settings.transform = action; | ||
return copy(obj, | ||
getClass(obj) === "Array" ? [] : {}, | ||
settings); | ||
} | ||
// Exports | ||
module.exports = { | ||
change: change, | ||
checkField: checkField, | ||
copy: copy, | ||
empty: empty, | ||
fromArray: fromArray, | ||
@@ -587,3 +897,7 @@ getClass: getClass, | ||
isSizeMore: isSizeMore, | ||
split: split | ||
map: map, | ||
remove: remove, | ||
reverse: reverse, | ||
split: split, | ||
transform: transform | ||
}; |
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
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3075110
100945
234