json-mapper
Just a simple json mapper.
How to use
Very simple case:
var input = user: name: 'John' nick: 'C00lHacker' ; var JM = ; var converter = JM; var result = ; console; // should be {name: 'John'}
Let's add a bit sugar by using factory method getVal
var converter = JM;
The syntax 'user.name'
equals JM.getVal('user.name')
var converter = JM;
If you want to chain callbacks use ch
factory
var input = user: name: 'Alex' nickname: 'FOfan' locations: x:1 y:21 // i need this x x:2 y:22 x:3 y:23 x:4 y:24 x:5 y:25 x:6 y:26 x:7 y:27 x:8 y:28 x:9 y:29 x:10 y:30 x:11 y:31 x:12 y:32 uuid: 'ffffffff-aaaaaaaa-c0c0afafc1c1fefe0-cfcf1234'; var JM = ;var converter = JM; var result = ; // should be {val: 1}
This stuff can be simplified by using array, e.g.:
var input = user: name: 'Alex' nickname: 'FOfan' locations: x:1 y:21 // i need this x x:2 y:22 x:3 y:23 x:4 y:24 x:5 y:25 x:6 y:26 x:7 y:27 x:8 y:28 x:9 y:29 x:10 y:30 x:11 y:31 x:12 y:32 uuid: 'ffffffff-aaaaaaaa-c0c0afafc1c1fefe0-cfcf1234'; var JM = ; var converter = JM; var result = ; // should be {val: 1}
JM.ch
function also can convert 'some.path' to JM.getVal('some.path')
.
There is a map factory for arrays processing.
var input = user: name: 'Alex' nickname: 'FOfan' locations: x:1 y:21 // i need this x x:2 y:22 x:3 y:23 x:4 y:24 x:5 y:25 x:6 y:26 x:7 y:27 x:8 y:28 x:9 y:29 x:10 y:30 x:11 y:31 x:12 y:32 uuid: 'ffffffff-aaaaaaaa-c0c0afafc1c1fefe0-cfcf1234'; var JM = ; var converter = JM; var result = ; // should be {val: [1,2,3,4,5,6,7,8,9,10,11,12]}
or
var converter = JM;
Use JM.makeCb(val)
to convert path
to getVal
Returning map:
input | return |
---|---|
function | function |
string | getVal(val) |
array | ch.apply(null,val) |
hash | schema(val) |
New feature is '$root'
alias
var JM = ; var input = uuid: '1233123123' user: name: 'sergey' objects: 'atoken' 'btoken' 'ctoken' 'dtoken' 'etoken' 'Fplane' 'Splane' 'nodejs' 'memcache' 'sql' 'tpl' 'ej' ; var converter = JM; console;
Result:
originalObject: uuid: '1233123123' user: name: 'sergey' objects: 'atoken' 'btoken' 'ctoken' 'dtoken' 'etoken' 'Fplane' 'Splane' 'nodejs' 'memcache' 'sql' 'tpl' 'ejs' uuid: '1233123123' link: '<a href="http://127.0.0.1/users/?name=sergey">user</a>' objects: 'http://127.0.0.1/objects/atoken' 'http://127.0.0.1/objects/btoken' 'http://127.0.0.1/objects/ctoken' 'http://127.0.0.1/objects/dtoken' 'http://127.0.0.1/objects/etoken' 'http://127.0.0.1/objects/Fplane' 'http://127.0.0.1/objects/Splane' 'http://127.0.0.1/objects/nodejs' 'http://127.0.0.1/objects/memcache' 'http://127.0.0.1/objects/sql' 'http://127.0.0.1/objects/tpl' 'http://127.0.0.1/objects/ejs'
Shut up and show me a SIMPLE convertion
var input = user: name: 'Alex' nickname: 'FOfan' locations: x:1 y:21 // i need this x x:2 y:22 x:3 y:23 x:4 y:24 x:5 y:25 x:6 y:26 x:7 y:27 x:8 y:28 x:9 y:29 x:10 y:30 x:11 y:31 x:12 y:32 uuid: 'ffffffff-aaaaaaaa-c0c0afafc1c1fefe0-cfcf1234'; var JM = ;var converter = JM; var result = ; console;
Result:
Helpers
template and templateStrong
Just an example:
var JM = ; var input = uuid: '1233123123' user: name: 'sergey' objects: id: 1001 name: 'atoken' id: 1002 name: 'btoken' id: 1003 name: 'ctoken' id: 1004 name: 'dtoken' id: 1005 name: 'etoken' id: 1006 name: 'Fplane' id: 1007 name: 'Splane' id: 1008 name: 'nodejs' id: 1009 name: 'memcache' id: 1010 name: 'sql' id: 1011 name: 'tpl' id: 1012 name: 'ej' ; var converter = JM; console;
Result:
uuid: '1233123123' hrefStrong: 'http://127.0.0.1/users/?name=sergey' href: 'http://127.0.0.1/users/?name=sergey' hreffail: 'http://127.0.0.1/users/?name=undefined' objects: href: 'http://127.0.0.1/objects/1001' href: 'http://127.0.0.1/objects/1002' href: 'http://127.0.0.1/objects/1003' href: 'http://127.0.0.1/objects/1004' href: 'http://127.0.0.1/objects/1005' href: 'http://127.0.0.1/objects/1006' href: 'http://127.0.0.1/objects/1007' href: 'http://127.0.0.1/objects/1008' href: 'http://127.0.0.1/objects/1009' href: 'http://127.0.0.1/objects/1010' href: 'http://127.0.0.1/objects/1011' href: 'http://127.0.0.1/objects/1012'
templateStrong
will return undefined if there is undefined keys
def
var JM = ; var converter = JM; console;
Result:
uuid: '14'
JM.helpers.def(val)
- always returns val
valOrDef
var JM = ; var converter = JM; console;
Result:
uuid: '14' uuid2: '17'
If input is null or undefined JM.helpers.valOrDef(val)
will return val, otherwise input will be returned.
dict
var JM = ; var converter = JM; console;
Result:
type: 'fit'
toBoolean, toNumber, toString, toUndefined, filterUndefined
var JM = ;var h = JMhelpers; var converter = JM; console;
Result is:
isGuest: true role: '2' userId: 13 catalogId: NaN catalogId3: 'somethingLiteral1'
Dict creates a dictionary and returns value by key.
to run unit test run
npm test
v0.0.12
- fixed package.json (thanks to alissonperez)
v0.0.11
sorry i've skipped several versions
- Add toString helper and fix unit tests (by alissonperez)
- clean package.json (by ahiipsa)
v0.0.9
- fix for #10 "makeConverter modifies its arguments"
- Add makeMapConverter (alexwhitman)
v0.0.8
- make performance optimizations
- add simple speed test "npm run speed"
- some results https://docs.google.com/spreadsheets/d/1VO_DpwQq8RKOMKlN9RIXHjblha6n58qBpKWWGardIJo/edit?usp=sharing
v0.0.7
- add unit tests
- minor changes
v0.0.6
- add example 'sentence test'
- modify readme (make this more readable ;)
- change jsdoc for functions
in feature
- make normal unit tests and try to do some optimization for more performance
- write more usage examples