Comparing version 0.2.6 to 0.2.7
@@ -12,4 +12,7 @@ declare module "like-ar"{ | ||
function likeAr<T>(o:{[key:string]:T}):ObjectWithArrayFunctions<T> | ||
namespace likeAr{} | ||
namespace likeAr{ | ||
function toPlainObject<T>(array:[string, T][]):{[key:string]:T} | ||
function toPlainObject<T, K extends keyof T>(array:T, keyName:K, valueName:K):{[key:string]:T} | ||
} | ||
export = likeAr | ||
} |
@@ -141,4 +141,18 @@ "use strict"; | ||
likeAr.toPlainObject = function toPlainObject(pairs, keyName, valueName){ | ||
var o={}; | ||
pairs.forEach(function(pair, i){ | ||
if(keyName==null){ | ||
keyName=0; | ||
valueName=valueName||1; | ||
}else if(valueName==null){ | ||
valueName='value'; | ||
} | ||
o[pair[keyName]]=pair[valueName]; | ||
}); | ||
return o; | ||
}; | ||
return likeAr; | ||
}); |
{ | ||
"name": "like-ar", | ||
"description": "Using objects like arrays with map, filter, forEach and others coming soon.", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"author": "Codenautas <codenautas@googlegroups.com>", | ||
@@ -6,0 +6,0 @@ "repository": "codenautas/like-ar", |
@@ -10,5 +10,3 @@ # like-ar | ||
[![coverage](https://img.shields.io/coveralls/codenautas/like-ar/master.svg)](https://coveralls.io/r/codenautas/like-ar) | ||
[![climate](https://img.shields.io/codeclimate/github/codenautas/like-ar.svg)](https://codeclimate.com/github/codenautas/like-ar) | ||
[![dependencies](https://img.shields.io/david/codenautas/like-ar.svg)](https://david-dm.org/codenautas/like-ar) | ||
[![qa-control](http://codenautas.com/github/codenautas/like-ar.svg)](http://codenautas.com/github/codenautas/like-ar) | ||
@@ -38,3 +36,4 @@ | ||
lastName:'Perez', | ||
firstName:'Diego' | ||
firstName:'Diego', | ||
phone:'+45-11-2222-3333' | ||
} | ||
@@ -45,5 +44,16 @@ | ||
}); | ||
console.log( | ||
likeAr(object).filter(function(value, attrName){ | ||
return attrName.contains('Name'); | ||
}).map(function(value,attrName){ | ||
return attrName+':'+value | ||
}).join(', ') | ||
); | ||
``` | ||
# API | ||
## likeAr(object) | ||
The callback functions receive these parameters: `value`, `key` and the original object. | ||
@@ -60,16 +70,23 @@ The functions that in the Array case returns Arrays returns a chainable object. | ||
`keys()` | array of keys | ||
`plain()` | plain array | ||
`plain()` | plain object without likeAr functions | ||
## likeAr.toPlainObject(array [,keyName [,valueName]]) | ||
Returns a plain object from an array of pairs. | ||
Default values: `0` and `1` if `keyName` is not set. `"value"` for `valueName` if `keyName` is set. | ||
# Usage | ||
```sh | ||
var likeAr = require('like-ar'); | ||
var object={ | ||
lastName:'Perez', | ||
firstName:'Diego' | ||
} | ||
var pairs=[['lastName', 'Perez'], ['firstName', 'Diego']]; | ||
likeAr(object).forEach(function(value, attrName, object){ | ||
console.log(attrName,':',value); | ||
}); | ||
console.log(likeAr.toPlainObject(pairs)); | ||
var pairs=[{field:'lastName', value:'Perez'}, {field:'firstName', value:'Diego'}]; | ||
console.log(likeAr.toPlainObject(pairs, 'field')); | ||
``` | ||
@@ -76,0 +93,0 @@ |
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
11138
152
94