{"id":13746957,"url":"https://github.com/dregenor/jsonMapper","last_synced_at":"2025-05-09T08:30:52.244Z","repository":{"id":13428056,"uuid":"16116826","full_name":"dregenor/jsonMapper","owner":"dregenor","description":"simple json mapper","archived":false,"fork":false,"pushed_at":"2017-07-10T15:00:18.000Z","size":57,"stargazers_count":31,"open_issues_count":1,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-14T10:53:54.385Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dregenor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-21T19:47:53.000Z","updated_at":"2023-10-28T21:52:14.000Z","dependencies_parsed_at":"2022-09-26T19:13:39.339Z","dependency_job_id":null,"html_url":"https://github.com/dregenor/jsonMapper","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dregenor%2FjsonMapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dregenor%2FjsonMapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dregenor%2FjsonMapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dregenor%2FjsonMapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dregenor","download_url":"https://codeload.github.com/dregenor/jsonMapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224842552,"owners_count":17378989,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T06:01:07.646Z","updated_at":"2024-11-15T20:30:51.052Z","avatar_url":"https://github.com/dregenor.png","language":"JavaScript","readme":"json-mapper\n==========\n\nJust a simple json mapper.\n\n- [How to use](#how-to-use)\n- [Shut up and show me a SIMPLE convertion](#shut-up-and-show-me-a-simple-convertion)\n- [Helpers](#helpers)\n\n\nHow to use\n----------\n\nVery simple case:\n\n\n```js\n\nvar input = {\n    user: {\n        name: 'John',\n        nick: 'C00lHacker'\n    }\n};\n\nvar JM = require('json-mapper');\n\nvar converter = JM.makeConverter({\n    name: function(input){\n            if (!input.user){\n                return;\n            } else {\n                return input.user.name;\n            }\n    }\n});\n\nvar result = converter(input);\n\nconsole.log(result); // should be {name: 'John'}\n\n```\n\nLet's add a bit sugar by using factory method `getVal` \n\n```js\n\nvar converter = JM.makeConverter({\n    name: JV.getVal('user.name');\n});\n\n```\n\nThe syntax `'user.name'` equals `JM.getVal('user.name')`\n\n```js\n\nvar converter = JM.makeConverter({\n    name: 'user.name';\n});\n\n```\n\nIf you want to chain callbacks use `ch` factory\n\n```js\n\nvar input = {\n  user: {\n      name: 'Alex',\n      nickname: 'FOfan'\n  },\n  locations: [\n      {x:1, y:21}, // i need this x\n      {x:2, y:22},\n      {x:3, y:23},\n      {x:4, y:24},\n      {x:5, y:25},\n      {x:6, y:26},\n      {x:7, y:27},\n      {x:8, y:28},\n      {x:9, y:29},\n      {x:10, y:30},\n      {x:11, y:31},\n      {x:12, y:32}\n  ],\n  uuid: 'ffffffff-aaaaaaaa-c0c0afafc1c1fefe0-cfcf1234'\n};\n\n\nvar JM = require('json-mapper');\nvar converter = JM.makeConverter({\n    val: JM.ch(\n                function(input){ return input.locations; },\n                function(input){ return input[0]; },\n                function(input){ return input.x; }\n            )\n});\n\nvar result = converter(input); // should be {val: 1}\n\n```\nThis stuff can be simplified by using array, e.g.:\n\n```js\n\nvar input = {\n  user: {\n      name: 'Alex',\n      nickname: 'FOfan'\n  },\n  locations: [\n      {x:1, y:21}, // i need this x\n      {x:2, y:22},\n      {x:3, y:23},\n      {x:4, y:24},\n      {x:5, y:25},\n      {x:6, y:26},\n      {x:7, y:27},\n      {x:8, y:28},\n      {x:9, y:29},\n      {x:10, y:30},\n      {x:11, y:31},\n      {x:12, y:32}\n  ],\n  uuid: 'ffffffff-aaaaaaaa-c0c0afafc1c1fefe0-cfcf1234'\n};\n\n\nvar JM = require('json-mapper');\n\nvar converter = JM.makeConverter({\n    val: [\n        function(input){ return input.locations; },\n        function(input){ return input[0]; },\n        function(input){ return input.x; }\n    ]\n});\n\nvar result = converter(input); // should be {val: 1}\n\n```\n\n`JM.ch` function also can convert 'some.path' to `JM.getVal('some.path')`.  \nThere is a map factory for arrays processing.\n\n```js\n\nvar input = {\n    user: {\n        name: 'Alex',\n        nickname: 'FOfan'\n    },\n    locations: [\n        {x:1, y:21}, // i need this x\n        {x:2, y:22},\n        {x:3, y:23},\n        {x:4, y:24},\n        {x:5, y:25},\n        {x:6, y:26},\n        {x:7, y:27},\n        {x:8, y:28},\n        {x:9, y:29},\n        {x:10, y:30},\n        {x:11, y:31},\n        {x:12, y:32}\n    ],\n    uuid: 'ffffffff-aaaaaaaa-c0c0afafc1c1fefe0-cfcf1234'\n};\n\n\nvar JM = require('json-mapper');\n\nvar converter = JM.makeConverter({\n    val: JM.ch('locations', JM.map(function(input){ return input.x; }))\n});\n\nvar result = converter(input); // should be {val: [1,2,3,4,5,6,7,8,9,10,11,12]}\n\n```\n\nor\n\n```js\n\nvar converter = JM.makeConverter({\n    val: ['locations', JM.map('x')]\n});\n    \n```\n\nUse `JM.makeCb(val)` to convert `path` to `getVal`\n\nReturning map:\n\n| input | return |\n|:-----:|:------:|\n| function | function |\n| string | getVal(val) |\n| array | ch.apply(null,val) |\n| hash  | schema(val) |\n\n\nNew feature is `'$root'` alias\n\n```js\n\nvar JM = require('json-mapper');\n\nvar input = {\n    uuid: '1233123123',\n    user: {\n        name: 'sergey'\n    },\n    objects: [\n        'atoken',\n        'btoken',\n        'ctoken',\n        'dtoken',\n        'etoken',\n        'Fplane',\n        'Splane',\n        'nodejs',\n        'memcache',\n        'sql',\n        'tpl',\n        'ej'\n    ]\n};\n\nvar converter = JM.makeConverter({\n    originalObject: '$root',\n    uuid: 'uuid',\n    link: [\n        JM.helpers.templateStrong('http://127.0.0.1/users/?name={user.name}'),\n        JM.helpers.templateStrong('\u003ca href=\"{$root}\"\u003euser\u003c/a\u003e')\n    ],\n    objects: ['objects', JM.map(JM.helpers.templateStrong('http://127.0.0.1/objects/{$root}'))]\n});\n\nconsole.log('\\n\\n\\n\\ convert with template \u0026 root', converter(input));\n\n```\nResult:\n\n```js\n\n{\n    originalObject: {\n        uuid: '1233123123',\n        user: {name: 'sergey'},\n        objects: [\n            'atoken',\n            'btoken',\n            'ctoken',\n            'dtoken',\n            'etoken',\n            'Fplane',\n            'Splane',\n            'nodejs',\n            'memcache',\n            'sql',\n            'tpl',\n            'ejs'\n        ]\n    },\n    uuid: '1233123123',\n    link: '\u003ca href=\"http://127.0.0.1/users/?name=sergey\"\u003euser\u003c/a\u003e',\n    objects: [\n        'http://127.0.0.1/objects/atoken',\n        'http://127.0.0.1/objects/btoken',\n        'http://127.0.0.1/objects/ctoken',\n        'http://127.0.0.1/objects/dtoken',\n        'http://127.0.0.1/objects/etoken',\n        'http://127.0.0.1/objects/Fplane',\n        'http://127.0.0.1/objects/Splane',\n        'http://127.0.0.1/objects/nodejs',\n        'http://127.0.0.1/objects/memcache',\n        'http://127.0.0.1/objects/sql',\n        'http://127.0.0.1/objects/tpl',\n        'http://127.0.0.1/objects/ejs'\n    ]\n}\n\n```\n\nShut up and show me a SIMPLE convertion\n--------\n\n```js\n\nvar input = {\n  user: {\n      name: 'Alex',\n      nickname: 'FOfan'\n  },\n  locations: [\n      {x:1, y:21}, // i need this x\n      {x:2, y:22},\n      {x:3, y:23},\n      {x:4, y:24},\n      {x:5, y:25},\n      {x:6, y:26},\n      {x:7, y:27},\n      {x:8, y:28},\n      {x:9, y:29},\n      {x:10, y:30},\n      {x:11, y:31},\n      {x:12, y:32}\n  ],\n  uuid: 'ffffffff-aaaaaaaa-c0c0afafc1c1fefe0-cfcf1234'\n};\n\n\nvar JM = require('json-mapper');\nvar converter = JM.makeConverter({\n    all_x: ['locations', JM.map('x')],\n    all_y: ['locations', JM.map('y')],\n    x_sum_y: ['locations', JM.map(function(input){\n        return input.x + input.y;\n    })],\n    locations_count: ['locations', function(arr){\n        return arr.length;\n    }],\n    locations_count_hack: 'locations.length',\n    just_mappet_name: 'user.name',\n    another_object: {\n        nickname: 'user.nickname',\n        location_0_x: 'locations.0.x'\n    }\n});\n\nvar result = converter(input);\n\nconsole.log(result);\n\n```\nResult:\n\n```json\n\n{\n  \"all_x\":   [ 1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12 ],\n  \"all_y\":   [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 ],\n  \"x_sum_y\": [ 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44 ],\n  \"locations_count\": 12,\n  \"locations_count_hack\": 12,\n  \"just_mappet_name\": \"Alex\",\n  \"another_object\": {\n    \"nickname\": \"FOfan\",\n    \"location_0_x\": 1\n  }\n}\n\n```\n\nHelpers\n========\n\ntemplate and templateStrong\n---------\n\nJust an example:\n\n```js\n\nvar JM = require('json-mapper');\n\nvar input = {\n    uuid: '1233123123',\n    user: {\n        name: 'sergey'\n    },\n    objects: [\n        {id: 1001, name: 'atoken'},\n        {id: 1002, name: 'btoken'},\n        {id: 1003, name: 'ctoken'},\n        {id: 1004, name: 'dtoken'},\n        {id: 1005, name: 'etoken'},\n        {id: 1006, name: 'Fplane'},\n        {id: 1007, name: 'Splane'},\n        {id: 1008, name: 'nodejs'},\n        {id: 1009, name: 'memcache'},\n        {id: 1010, name: 'sql'},\n        {id: 1011, name: 'tpl'},\n        {id: 1012, name: 'ej'}\n    ]\n};\n\n\nvar converter = JM.makeConverter({\n    uuid:           'uuid',\n    hrefStrong:     JM.helpers.templateStrong('http://127.0.0.1/users/?name={user.name}'),\n    href:           JM.helpers.template('http://127.0.0.1/users/?name={user.name}'),\n    hrefStrongFail: JM.helpers.templateStrong('http://127.0.0.1/users/?name={user.undefinedKey}'),\n    hreffail:       JM.helpers.template('http://127.0.0.1/users/?name={user.undefinedKey}'),\n    objects: ['objects', JM.map({\n        href: JM.helpers.templateStrong('http://127.0.0.1/objects/{id}')\n    })]\n});\n\nconsole.log('\\n\\n\\n convert with template \\n\\n', converter(input));\n\n```\n\nResult:\n\n```js\n\n{\n    uuid:        '1233123123',\n    hrefStrong:  'http://127.0.0.1/users/?name=sergey',\n    href:        'http://127.0.0.1/users/?name=sergey',\n    hreffail:    'http://127.0.0.1/users/?name=undefined',\n    objects: [\n        { href: 'http://127.0.0.1/objects/1001' },\n        { href: 'http://127.0.0.1/objects/1002' },\n        { href: 'http://127.0.0.1/objects/1003' },\n        { href: 'http://127.0.0.1/objects/1004' },\n        { href: 'http://127.0.0.1/objects/1005' },\n        { href: 'http://127.0.0.1/objects/1006' },\n        { href: 'http://127.0.0.1/objects/1007' },\n        { href: 'http://127.0.0.1/objects/1008' },\n        { href: 'http://127.0.0.1/objects/1009' },\n        { href: 'http://127.0.0.1/objects/1010' },\n        { href: 'http://127.0.0.1/objects/1011' },\n        { href: 'http://127.0.0.1/objects/1012' }\n    ]\n}\n\n```\n\n`templateStrong` will return undefined if there is undefined keys\n\ndef\n----\n\n```js\n\nvar JM = require('json-mapper');\n\nvar converter = JM.makeConverter({\n    uuid: JM.helpers.def('14')\n});\n\nconsole.log('\\n\\n\\n convert with default \\n\\n', converter({}));\n\n```\n\nResult:\n\n```js\n\n{\n    uuid: '14'\n}\n\n```\n\n`JM.helpers.def(val)` - always returns val\n\n\nvalOrDef\n-----\n\n```js\n\nvar JM = require('json-mapper');\n\nvar converter = JM.makeConverter({\n    uuid:  [ 'uuid' , JM.helpers.def('14') ],\n    uuid2: [ 'uuid2', JM.helpers.valOrDef('15') ]\n});\n\nconsole.log('\\n\\n\\n convert with default \\n\\n', converter({\n    'uuid': '15',\n    'uuid2': '17'\n}));\n\n```\n\nResult:\n\n```js\n\n{\n    uuid: '14',\n    uuid2: '17'\n}\n\n```\n\nIf input is null or undefined `JM.helpers.valOrDef(val)` will return val, otherwise input will be returned.\n\n\ndict\n----\n\n```js\n\nvar JM = require('json-mapper');\n\nvar converter = JM.makeConverter({\n    type: [\n        'type' ,\n        JM.helpers.dict({\n            1: 'fit',\n            2: 'crop',\n            3: 'fit'\n        })\n    ]\n});\n\nconsole.log('\\n\\n\\n convert with default \\n\\n', converter({\n    'type': 1\n}));\n\n```\nResult:\n\n```js\n\n{\n    type: 'fit'\n}\n\n```\n\ntoBoolean, toNumber, toString, toUndefined, filterUndefined\n----------------------------------------------\n\n```js\n\nvar JM = require('json-mapper');\nvar h = JM.helpers;\n\nvar converter = JM.makeConverter({\n    isGuest: ['role', h.toBoolean],\n    isUser: ['user', h.toBoolean],\n    role: ['role', h.toString],\n    userId: ['userId', h.toNumber],\n    catalogId: ['catalogId', h.toNumber],\n    catalogId2: ['catalogId', h.toNumber, h.toUndefined],\n    catalogId3: ['catalogId', h.filterUndefined(function(input){\n        // input always not undefined\n        return input + '1';\n    })],\n    catalogId4: ['UndefinedCatalogId', h.filterUndefined(function(input){\n        // input always not undefined\n        return input + '1';\n    })]\n});\n\n\nconsole.log('\\n\\n\\n convert to boolean and to number \\n\\n', converter({\n    \"role\": 2,\n    \"userId\": '13',\n    \"catalogId\": 'somethingLiteral'\n}));\n\n```\n\nResult is:\n\n```js\n\n{\n    isGuest: true,\n    role: '2',\n    userId: 13,\n    catalogId: NaN,\n    catalogId3: 'somethingLiteral1'\n}\n\n```\n\nDict creates a dictionary and returns value by key.\n\n\nto run unit test run\n\n```\n   npm test\n```\n\nv0.0.12\n- fixed package.json (thanks to alissonperez)\n\nv0.0.11\n-------\nsorry i've skipped several versions\n\n- Add toString helper and fix unit tests (by alissonperez)\n- clean package.json (by ahiipsa)\n\nv0.0.9\n- fix for #10 \"makeConverter modifies its arguments\"\n- Add makeMapConverter (alexwhitman)\n\nv0.0.8\n------\n\n- make performance optimizations\n- add simple speed test \"npm run speed\"\n- some results https://docs.google.com/spreadsheets/d/1VO_DpwQq8RKOMKlN9RIXHjblha6n58qBpKWWGardIJo/edit?usp=sharing\n\n\nv0.0.7\n-----\n\n- add unit tests \n- minor changes\n\n\nv0.0.6\n------\n\n- add example 'sentence test'\n- modify readme (make this more readable ;)\n- change jsdoc for functions\n \n \nin feature\n-----\n\n- make normal unit tests and try to do some optimization for more performance \n- write more usage examples","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdregenor%2FjsonMapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdregenor%2FjsonMapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdregenor%2FjsonMapper/lists"}