├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── changelog.md ├── index.js ├── license ├── package-lock.json ├── package.json ├── readme.md └── test ├── apex-nitro-test.js └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Folders 2 | node_modules 3 | 4 | # Logs 5 | logs 6 | *.log 7 | npm-debug.log* 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "8" 4 | - "9" 5 | - "10" 6 | os: 7 | - linux 8 | - osx 9 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 1.1.4 4 | 5 | - Travis build fix 6 | 7 | ## 1.1.3 8 | 9 | - Dependency performance & size 10 | 11 | ## 1.1.2 12 | 13 | - Bug fixes 14 | 15 | ## 1.1.1 16 | 17 | - Minor things 18 | - Documentation 19 | - Code comments 20 | - More tests 21 | 22 | ## 1.1.0 23 | 24 | - new feature: dependsOn 25 | - allow to change keys based on determined predicates 26 | 27 | ## 1.0.0 28 | 29 | - Initial Release 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const get = require('lodash.get'); 4 | const set = require('lodash.set'); 5 | const unset = require('lodash.unset'); 6 | 7 | module.exports = { 8 | map(json, mapping) { 9 | // Copy the arguments 10 | const jsonMapped = Object.assign({}, json); 11 | const maps = Object.assign([], mapping); 12 | 13 | // Loop through the mapping parameters 14 | // Each iteration represent a structural change to argument "json" 15 | Object.keys(maps).forEach(key => { 16 | const oldKeyValue = get(jsonMapped, maps[key].oldKey); 17 | let newKeyValue = oldKeyValue; 18 | const {values} = maps[key]; 19 | const {dependsOn} = maps[key]; 20 | 21 | // Add the new values 22 | if (values) { 23 | for (const value_ of values) { 24 | if (oldKeyValue === value_.oldValue) { 25 | newKeyValue = value_.newValue; 26 | break; 27 | } else { 28 | newKeyValue = undefined; 29 | } 30 | } 31 | } 32 | 33 | // Add dependencies 34 | if (dependsOn) { 35 | const dependsOnKey = get(jsonMapped, dependsOn.key); 36 | 37 | if (dependsOnKey === dependsOn.if && dependsOn.ifValue) { 38 | set(jsonMapped, maps[key].newKey, dependsOn.ifValue); 39 | } else if (dependsOn.elseValue) { 40 | set(jsonMapped, maps[key].newKey, dependsOn.elseValue); 41 | } 42 | } 43 | 44 | // Set the new object in the new json structure 45 | if (newKeyValue !== undefined && 46 | maps[key].newKey !== null && 47 | maps[key].newKey !== undefined && 48 | maps[key].newKey !== '') { 49 | set(jsonMapped, maps[key].newKey, newKeyValue); 50 | } 51 | 52 | // Removes the old key if the name has changed 53 | if (maps[key].oldKey !== maps[key].newKey) { 54 | unset(jsonMapped, maps[key].oldKey); 55 | } 56 | }); 57 | 58 | return jsonMapped; 59 | } 60 | }; 61 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Vincent Morneau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-mapping", 3 | "version": "1.1.5", 4 | "description": "Small module to map a JSON structure to a new structure", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "scripts": { 8 | "test": "xo && ava" 9 | }, 10 | "keywords": [ 11 | "json", 12 | "mapping" 13 | ], 14 | "repository": { 15 | "type": "git", 16 | "url": "https://github.com/vincentmorneau/json-mapping.git" 17 | }, 18 | "bugs": { 19 | "url": "https://github.com/vincentmorneau/json-mapping/issues" 20 | }, 21 | "devDependencies": { 22 | "ava": "^3.11.1", 23 | "lodash.isequal": "^4.5.0", 24 | "xo": "^0.32.1" 25 | }, 26 | "dependencies": { 27 | "lodash.get": "^4.4.2", 28 | "lodash.set": "^4.3.2", 29 | "lodash.unset": "^4.5.2" 30 | }, 31 | "xo": { 32 | "ignores": [ 33 | "./test/apex-nitro-test.js", 34 | "./test/test.js" 35 | ] 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # JSON Mapping 2 | 3 | [![npm](https://img.shields.io/npm/v/json-mapping.svg)]() [![Build Status](https://travis-ci.org/vincentmorneau/json-mapping.svg?branch=master)](https://travis-ci.org/vincentmorneau/json-mapping) [![Dependency Status](https://david-dm.org/vincentmorneau/json-mapping.svg)](https://david-dm.org/vincentmorneau/json-mapping) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) 4 | 5 | Transform a JSON object structure. 6 | 7 | ## Install 8 | ``` 9 | npm install json-mapping 10 | ``` 11 | 12 | ## Usage 13 | ```javascript 14 | const mapping = require('json-mapping'); 15 | 16 | let json = { 17 | "appURL": "localhost", 18 | "object1": { 19 | "bool": true, 20 | "name": "app" 21 | } 22 | }; 23 | 24 | json = mapping.map(json, [ 25 | { 26 | oldKey: "appURL", 27 | newKey: "url" 28 | }, 29 | { 30 | oldKey: "object1.bool", 31 | newKey: "object1.enabled" 32 | } 33 | ]); 34 | /* 35 | { 36 | "url": "localhost", 37 | "object1": { 38 | "enabled": true, 39 | "name": "app" 40 | } 41 | } 42 | */ 43 | ``` 44 | 45 | ## Arguments 46 | Name | Type | Description 47 | --- | --- | --- 48 | json | object | The initial JSON object to be mapped 49 | mapping* | array | An array containing the mapping options 50 | 51 | ###### \*mapping 52 | Name | Type | Description 53 | --- | --- | --- 54 | oldKey | string | The old property name to be mapped 55 | newKey | string | The new property name to be mapped 56 | values** | array | An array of mapped values for this property mapping 57 | dependsOn** | object | Determines the value for the newKey based on a condition 58 | 59 | ###### \*\*values 60 | Name | Type | Description 61 | --- | --- | --- 62 | oldValue | any | The old value of the property to be mapped 63 | newValue | any | The new value of the property to be mapped 64 | 65 | ###### \*\*dependsOn 66 | Name | Type | Description 67 | --- | --- | --- 68 | key | string | The key to look for 69 | if | any | The value to evaluate for the key 70 | ifValue | any | When key === if, this will be the value of the newKey 71 | elseValue | any | When key !== if, this will be the value of the newKey 72 | 73 | ## Methods 74 | Name | Type | Return | Description 75 | --- | --- | --- | --- 76 | map | function | object | Maps a JSON object using mapping options 77 | 78 | ## Changelog 79 | [See changelog.](changelog.md) 80 | 81 | ## License 82 | MIT © [Vincent Morneau](http://vmorneau.me) 83 | -------------------------------------------------------------------------------- /test/apex-nitro-test.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const test = require("ava"); 4 | const _app = require(".."); 5 | 6 | // This test suite is based on a real use case 7 | // https://github.com/OraOpenSource/apex-nitro/ 8 | 9 | const isEqual = require("lodash.isequal"); 10 | 11 | const mapping1 = [ 12 | { 13 | oldKey: "jsConcat.enabled", 14 | newKey: "js.concat", 15 | }, 16 | { 17 | oldKey: "jsConcat.finalName", 18 | newKey: "js.concatFilename", 19 | }, 20 | { 21 | oldKey: "cssConcat.enabled", 22 | newKey: "css.concat", 23 | }, 24 | { 25 | oldKey: "cssConcat.finalName", 26 | newKey: "css.concatFilename", 27 | }, 28 | { 29 | oldKey: "sass.includePath", 30 | newKey: "css.sassIncludePath", 31 | }, 32 | { 33 | oldKey: "less.includePath", 34 | newKey: "css.lessIncludePath", 35 | }, 36 | { 37 | oldKey: "sass.enabled", 38 | newKey: "css.language", 39 | values: [ 40 | { 41 | oldValue: true, 42 | newValue: "sass", 43 | }, 44 | ], 45 | }, 46 | { 47 | oldKey: "less.enabled", 48 | newKey: "css.language", 49 | values: [ 50 | { 51 | oldValue: true, 52 | newValue: "less", 53 | }, 54 | ], 55 | }, 56 | { 57 | oldKey: "jsConcat", 58 | }, 59 | { 60 | oldKey: "cssConcat", 61 | }, 62 | { 63 | oldKey: "sass", 64 | }, 65 | { 66 | oldKey: "less", 67 | }, 68 | ]; 69 | 70 | const mapping2 = [ 71 | { 72 | oldKey: "jsConcat.enabled", 73 | newKey: "js.concat", 74 | }, 75 | { 76 | oldKey: "jsConcat.finalName", 77 | newKey: "js.concatFilename", 78 | }, 79 | { 80 | oldKey: "cssConcat.enabled", 81 | newKey: "css.concat", 82 | }, 83 | { 84 | oldKey: "cssConcat.finalName", 85 | newKey: "css.concatFilename", 86 | }, 87 | { 88 | oldKey: "sass.includePath", 89 | newKey: "css.sassIncludePath", 90 | }, 91 | { 92 | oldKey: "less.includePath", 93 | newKey: "css.lessIncludePath", 94 | }, 95 | { 96 | oldKey: "sass.enabled", 97 | newKey: "css.language", 98 | values: [ 99 | { 100 | oldValue: true, 101 | newValue: "sass", 102 | }, 103 | ], 104 | }, 105 | { 106 | oldKey: "less.enabled", 107 | newKey: "css.language", 108 | values: [ 109 | { 110 | oldValue: true, 111 | newValue: "less", 112 | }, 113 | ], 114 | }, 115 | { 116 | oldKey: "jsConcat", 117 | }, 118 | { 119 | oldKey: "cssConcat", 120 | }, 121 | { 122 | oldKey: "sass", 123 | }, 124 | { 125 | oldKey: "less", 126 | }, 127 | { 128 | newKey: "mode", 129 | dependsOn: { 130 | key: "distFolder", 131 | if: undefined, 132 | ifValue: "basic", 133 | elseValue: "advanced", 134 | }, 135 | }, 136 | ]; 137 | 138 | const mapping3 = [ 139 | { 140 | oldKey: "jsConcat.enabled", 141 | newKey: "js.concat", 142 | }, 143 | { 144 | oldKey: "jsConcat.finalName", 145 | newKey: "js.concatFilename", 146 | }, 147 | { 148 | oldKey: "cssConcat.enabled", 149 | newKey: "css.concat", 150 | }, 151 | { 152 | oldKey: "cssConcat.finalName", 153 | newKey: "css.concatFilename", 154 | }, 155 | { 156 | oldKey: "sass.includePath", 157 | newKey: "css.sassIncludePath", 158 | }, 159 | { 160 | oldKey: "less.includePath", 161 | newKey: "css.lessIncludePath", 162 | }, 163 | { 164 | oldKey: "sass.enabled", 165 | newKey: "css.language", 166 | values: [ 167 | { 168 | oldValue: true, 169 | newValue: "sass", 170 | }, 171 | ], 172 | }, 173 | { 174 | oldKey: "less.enabled", 175 | newKey: "css.language", 176 | values: [ 177 | { 178 | oldValue: true, 179 | newValue: "less", 180 | }, 181 | ], 182 | }, 183 | { 184 | oldKey: "jsConcat", 185 | }, 186 | { 187 | oldKey: "cssConcat", 188 | }, 189 | { 190 | oldKey: "sass", 191 | }, 192 | { 193 | oldKey: "less", 194 | }, 195 | { 196 | newKey: "mode", 197 | dependsOn: { 198 | key: "distFolder", 199 | if: undefined, 200 | ifValue: "basic", 201 | elseValue: "advanced", 202 | }, 203 | }, 204 | { 205 | oldKey: "apex.apexDestination", 206 | newKey: "sqlcl.destination", 207 | }, 208 | { 209 | oldKey: "sqlcl", 210 | newKey: "publish", 211 | }, 212 | { 213 | oldKey: "publish.apexDestination", 214 | newKey: "publish.destination", 215 | }, 216 | ]; 217 | 218 | const mapping4 = [ 219 | { 220 | oldKey: "jsConcat.enabled", 221 | newKey: "js.concat", 222 | }, 223 | { 224 | oldKey: "jsConcat.finalName", 225 | newKey: "js.concatFilename", 226 | }, 227 | { 228 | oldKey: "cssConcat.enabled", 229 | newKey: "css.concat", 230 | }, 231 | { 232 | oldKey: "cssConcat.finalName", 233 | newKey: "css.concatFilename", 234 | }, 235 | { 236 | oldKey: "sass.includePath", 237 | newKey: "css.sassIncludePath", 238 | }, 239 | { 240 | oldKey: "less.includePath", 241 | newKey: "css.lessIncludePath", 242 | }, 243 | { 244 | oldKey: "sass.enabled", 245 | newKey: "css.language", 246 | values: [ 247 | { 248 | oldValue: true, 249 | newValue: "sass", 250 | }, 251 | ], 252 | }, 253 | { 254 | oldKey: "less.enabled", 255 | newKey: "css.language", 256 | values: [ 257 | { 258 | oldValue: true, 259 | newValue: "less", 260 | }, 261 | ], 262 | }, 263 | { 264 | oldKey: "jsConcat", 265 | }, 266 | { 267 | oldKey: "cssConcat", 268 | }, 269 | { 270 | oldKey: "sass", 271 | }, 272 | { 273 | oldKey: "less", 274 | }, 275 | { 276 | newKey: "mode", 277 | dependsOn: { 278 | key: "distFolder", 279 | if: undefined, 280 | ifValue: "basic", 281 | elseValue: "advanced", 282 | }, 283 | }, 284 | { 285 | oldKey: "apex.apexDestination", 286 | newKey: "sqlcl.destination", 287 | }, 288 | { 289 | oldKey: "sqlcl", 290 | newKey: "publish", 291 | }, 292 | { 293 | oldKey: "publish.apexDestination", 294 | newKey: "publish.destination", 295 | }, 296 | { 297 | newKey: "browsersync.realTime", 298 | values: [ 299 | { 300 | newValue: true, 301 | }, 302 | ], 303 | }, 304 | ]; 305 | 306 | const object1 = { 307 | appURL: "http://localhost:32513/ords/f?p=430:1", 308 | srcFolder: "/Users/vmorneau/Documents/project/src", 309 | distFolder: "/Users/vmorneau/Documents/project/dist", 310 | js: { 311 | processor: "default", 312 | concat: false, 313 | }, 314 | cssConcat: { 315 | enabled: true, 316 | finalName: "app", 317 | }, 318 | sass: { 319 | enabled: true, 320 | includePath: "/Users/vmorneau/Documents/bootstrap/sass", 321 | }, 322 | browsersync: { 323 | notify: true, 324 | ghostMode: false, 325 | }, 326 | header: { 327 | enabled: true, 328 | packageJsonPath: "/Users/vmorneau/Documents/project/package.json", 329 | }, 330 | apex: { 331 | openBuilder: true, 332 | apexDestination: "application", 333 | }, 334 | sqlcl: { 335 | path: "sql", 336 | }, 337 | }; 338 | 339 | const object2 = { 340 | appURL: "http://localhost:32513/ords/f?p=430:1", 341 | srcFolder: "/Users/vmorneau/Documents/project/src", 342 | distFolder: "/Users/vmorneau/Documents/project/dist", 343 | js: { 344 | processor: "default", 345 | concat: false, 346 | }, 347 | css: { 348 | language: "sass", 349 | sassIncludePath: "/Users/vmorneau/Documents/bootstrap/sass", 350 | concat: true, 351 | concatFilename: "app", 352 | }, 353 | browsersync: { 354 | notify: true, 355 | ghostMode: false, 356 | }, 357 | header: { 358 | enabled: true, 359 | packageJsonPath: "/Users/vmorneau/Documents/project/package.json", 360 | }, 361 | apex: { 362 | openBuilder: true, 363 | apexDestination: "application", 364 | }, 365 | sqlcl: { 366 | path: "sql", 367 | }, 368 | }; 369 | 370 | const object3 = { 371 | mode: "advanced", 372 | appURL: "http://localhost:32513/ords/f?p=430:1", 373 | srcFolder: "/Users/vmorneau/Documents/project/src", 374 | distFolder: "/Users/vmorneau/Documents/project/dist", 375 | js: { 376 | processor: "default", 377 | concat: false, 378 | }, 379 | css: { 380 | language: "sass", 381 | sassIncludePath: "/Users/vmorneau/Documents/bootstrap/sass", 382 | concat: true, 383 | concatFilename: "app", 384 | }, 385 | browsersync: { 386 | notify: true, 387 | ghostMode: false, 388 | }, 389 | header: { 390 | enabled: true, 391 | packageJsonPath: "/Users/vmorneau/Documents/project/package.json", 392 | }, 393 | apex: { 394 | openBuilder: true, 395 | apexDestination: "application", 396 | }, 397 | sqlcl: { 398 | path: "sql", 399 | }, 400 | }; 401 | 402 | const object4 = { 403 | mode: "advanced", 404 | appURL: "http://localhost:32513/ords/f?p=430:1", 405 | srcFolder: "/Users/vmorneau/Documents/project/src", 406 | distFolder: "/Users/vmorneau/Documents/project/dist", 407 | js: { 408 | processor: "default", 409 | concat: false, 410 | }, 411 | css: { 412 | language: "sass", 413 | sassIncludePath: "/Users/vmorneau/Documents/bootstrap/sass", 414 | concat: true, 415 | concatFilename: "app", 416 | }, 417 | browsersync: { 418 | notify: true, 419 | ghostMode: false, 420 | }, 421 | header: { 422 | enabled: true, 423 | packageJsonPath: "/Users/vmorneau/Documents/project/package.json", 424 | }, 425 | apex: { 426 | openBuilder: true, 427 | }, 428 | publish: { 429 | destination: "application", 430 | path: "sql", 431 | }, 432 | }; 433 | 434 | const object5 = { 435 | mode: "advanced", 436 | appURL: "http://localhost:32513/ords/f?p=430:1", 437 | srcFolder: "/Users/vmorneau/Documents/project/src", 438 | distFolder: "/Users/vmorneau/Documents/project/dist", 439 | js: { 440 | processor: "default", 441 | concat: false, 442 | }, 443 | css: { 444 | language: "sass", 445 | sassIncludePath: "/Users/vmorneau/Documents/bootstrap/sass", 446 | concat: true, 447 | concatFilename: "app", 448 | }, 449 | browsersync: { 450 | notify: true, 451 | ghostMode: false, 452 | realTime: true, 453 | }, 454 | header: { 455 | enabled: true, 456 | packageJsonPath: "/Users/vmorneau/Documents/project/package.json", 457 | }, 458 | apex: { 459 | openBuilder: true, 460 | }, 461 | publish: { 462 | destination: "application", 463 | path: "sql", 464 | }, 465 | }; 466 | 467 | test("apex-nitro-1.1", (t) => { 468 | const object = JSON.parse(JSON.stringify(object1)); 469 | const mapping = JSON.parse(JSON.stringify(mapping1)); 470 | const expected = JSON.parse(JSON.stringify(object2)); 471 | const mapped = _app.map(object, mapping); 472 | 473 | if (isEqual(mapped, expected)) { 474 | t.pass(); 475 | } else { 476 | t.log("expected:"); 477 | t.log(JSON.stringify(expected, null, "\t")); 478 | t.log("got:"); 479 | t.log(JSON.stringify(mapped, null, "\t")); 480 | t.fail(); 481 | } 482 | }); 483 | 484 | test("apex-nitro-1.2", (t) => { 485 | const object = JSON.parse(JSON.stringify(object1)); 486 | const mapping = JSON.parse(JSON.stringify(mapping2)); 487 | const expected = JSON.parse(JSON.stringify(object3)); 488 | const mapped = _app.map(object, mapping); 489 | 490 | if (isEqual(mapped, expected)) { 491 | t.pass(); 492 | } else { 493 | t.log("expected:"); 494 | t.log(JSON.stringify(expected, null, "\t")); 495 | t.log("got:"); 496 | t.log(JSON.stringify(mapped, null, "\t")); 497 | t.fail(); 498 | } 499 | }); 500 | 501 | test("apex-nitro-1.3", (t) => { 502 | const object = JSON.parse(JSON.stringify(object1)); 503 | const mapping = JSON.parse(JSON.stringify(mapping3)); 504 | const expected = JSON.parse(JSON.stringify(object4)); 505 | const mapped = _app.map(object, mapping); 506 | 507 | if (isEqual(mapped, expected)) { 508 | t.pass(); 509 | } else { 510 | t.log("expected:"); 511 | t.log(JSON.stringify(expected, null, "\t")); 512 | t.log("got:"); 513 | t.log(JSON.stringify(mapped, null, "\t")); 514 | t.fail(); 515 | } 516 | }); 517 | 518 | test("apex-nitro-1.4", (t) => { 519 | const object = JSON.parse(JSON.stringify(object1)); 520 | const mapping = JSON.parse(JSON.stringify(mapping4)); 521 | const expected = JSON.parse(JSON.stringify(object5)); 522 | const mapped = _app.map(object, mapping); 523 | 524 | if (isEqual(mapped, expected)) { 525 | t.pass(); 526 | } else { 527 | t.log("expected:"); 528 | t.log(JSON.stringify(expected, null, "\t")); 529 | t.log("got:"); 530 | t.log(JSON.stringify(mapped, null, "\t")); 531 | t.fail(); 532 | } 533 | }); 534 | 535 | test("apex-nitro-2.1", (t) => { 536 | const object = JSON.parse(JSON.stringify(object2)); 537 | const mapping = JSON.parse(JSON.stringify(mapping2)); 538 | const expected = JSON.parse(JSON.stringify(object3)); 539 | const mapped = _app.map(object, mapping); 540 | 541 | if (isEqual(mapped, expected)) { 542 | t.pass(); 543 | } else { 544 | t.log("expected:"); 545 | t.log(JSON.stringify(expected, null, "\t")); 546 | t.log("got:"); 547 | t.log(JSON.stringify(mapped, null, "\t")); 548 | t.fail(); 549 | } 550 | }); 551 | 552 | test("apex-nitro-2.2", (t) => { 553 | const object = JSON.parse(JSON.stringify(object2)); 554 | const mapping = JSON.parse(JSON.stringify(mapping3)); 555 | const expected = JSON.parse(JSON.stringify(object4)); 556 | const mapped = _app.map(object, mapping); 557 | 558 | if (isEqual(mapped, expected)) { 559 | t.pass(); 560 | } else { 561 | t.log("expected:"); 562 | t.log(JSON.stringify(expected, null, "\t")); 563 | t.log("got:"); 564 | t.log(JSON.stringify(mapped, null, "\t")); 565 | t.fail(); 566 | } 567 | }); 568 | 569 | test("apex-nitro-2.3", (t) => { 570 | const object = JSON.parse(JSON.stringify(object2)); 571 | const mapping = JSON.parse(JSON.stringify(mapping4)); 572 | const expected = JSON.parse(JSON.stringify(object5)); 573 | const mapped = _app.map(object, mapping); 574 | 575 | if (isEqual(mapped, expected)) { 576 | t.pass(); 577 | } else { 578 | t.log("expected:"); 579 | t.log(JSON.stringify(expected, null, "\t")); 580 | t.log("got:"); 581 | t.log(JSON.stringify(mapped, null, "\t")); 582 | t.fail(); 583 | } 584 | }); 585 | 586 | test("apex-nitro-3.1", (t) => { 587 | const object = JSON.parse(JSON.stringify(object3)); 588 | const mapping = JSON.parse(JSON.stringify(mapping3)); 589 | const expected = JSON.parse(JSON.stringify(object4)); 590 | const mapped = _app.map(object, mapping); 591 | 592 | if (isEqual(mapped, expected)) { 593 | t.pass(); 594 | } else { 595 | t.log("expected:"); 596 | t.log(JSON.stringify(expected, null, "\t")); 597 | t.log("got:"); 598 | t.log(JSON.stringify(mapped, null, "\t")); 599 | t.fail(); 600 | } 601 | }); 602 | 603 | test("apex-nitro-3.2", (t) => { 604 | const object = JSON.parse(JSON.stringify(object3)); 605 | const mapping = JSON.parse(JSON.stringify(mapping4)); 606 | const expected = JSON.parse(JSON.stringify(object5)); 607 | const mapped = _app.map(object, mapping); 608 | 609 | if (isEqual(mapped, expected)) { 610 | t.pass(); 611 | } else { 612 | t.log("expected:"); 613 | t.log(JSON.stringify(expected, null, "\t")); 614 | t.log("got:"); 615 | t.log(JSON.stringify(mapped, null, "\t")); 616 | t.fail(); 617 | } 618 | }); 619 | 620 | test("apex-nitro-4.1", (t) => { 621 | const object = JSON.parse(JSON.stringify(object4)); 622 | const mapping = JSON.parse(JSON.stringify(mapping4)); 623 | const expected = JSON.parse(JSON.stringify(object5)); 624 | const mapped = _app.map(object, mapping); 625 | 626 | if (isEqual(mapped, expected)) { 627 | t.pass(); 628 | } else { 629 | t.log("expected:"); 630 | t.log(JSON.stringify(expected, null, "\t")); 631 | t.log("got:"); 632 | t.log(JSON.stringify(mapped, null, "\t")); 633 | t.fail(); 634 | } 635 | }); 636 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const test = require("ava"); 4 | const _app = require(".."); 5 | 6 | const isEqual = require("lodash.isequal"); 7 | 8 | test("noop", (t) => { 9 | const mapped = _app.map(); 10 | const expected = {}; 11 | 12 | if (isEqual(mapped, expected)) { 13 | t.pass(); 14 | } else { 15 | t.log("expected:"); 16 | t.log(JSON.stringify(expected, null, "\t")); 17 | t.log("got:"); 18 | t.log(JSON.stringify(mapped, null, "\t")); 19 | t.fail(); 20 | } 21 | }); 22 | 23 | test("nomap", (t) => { 24 | const mapped = _app.map({ 25 | appURL: "localhost", 26 | object1: { 27 | bool: true, 28 | name: "app", 29 | }, 30 | }); 31 | const expected = { 32 | appURL: "localhost", 33 | object1: { 34 | bool: true, 35 | name: "app", 36 | }, 37 | }; 38 | 39 | if (isEqual(mapped, expected)) { 40 | t.pass(); 41 | } else { 42 | t.log("expected:"); 43 | t.log(JSON.stringify(expected, null, "\t")); 44 | t.log("got:"); 45 | t.log(JSON.stringify(mapped, null, "\t")); 46 | t.fail(); 47 | } 48 | }); 49 | 50 | test("simple", (t) => { 51 | const mapped = _app.map( 52 | { 53 | appURL: "localhost", 54 | object1: { 55 | bool: true, 56 | name: "app", 57 | }, 58 | }, 59 | [ 60 | { 61 | oldKey: "appURL", 62 | newKey: "url", 63 | }, 64 | ] 65 | ); 66 | const expected = { 67 | url: "localhost", 68 | object1: { 69 | bool: true, 70 | name: "app", 71 | }, 72 | }; 73 | 74 | if (isEqual(mapped, expected)) { 75 | t.pass(); 76 | } else { 77 | t.log("expected:"); 78 | t.log(JSON.stringify(expected, null, "\t")); 79 | t.log("got:"); 80 | t.log(JSON.stringify(mapped, null, "\t")); 81 | t.fail(); 82 | } 83 | }); 84 | 85 | test("deep-simple", (t) => { 86 | const mapped = _app.map( 87 | { 88 | appURL: "localhost", 89 | object1: { 90 | bool: true, 91 | name: "app", 92 | }, 93 | }, 94 | [ 95 | { 96 | oldKey: "object1.bool", 97 | newKey: "object1.enabled", 98 | }, 99 | ] 100 | ); 101 | const expected = { 102 | appURL: "localhost", 103 | object1: { 104 | enabled: true, 105 | name: "app", 106 | }, 107 | }; 108 | 109 | if (isEqual(mapped, expected)) { 110 | t.pass(); 111 | } else { 112 | t.log("expected:"); 113 | t.log(JSON.stringify(expected, null, "\t")); 114 | t.log("got:"); 115 | t.log(JSON.stringify(mapped, null, "\t")); 116 | t.fail(); 117 | } 118 | }); 119 | 120 | test("multiple", (t) => { 121 | const mapped = _app.map( 122 | { 123 | appURL: "localhost", 124 | object1: { 125 | bool: true, 126 | name: "app", 127 | }, 128 | }, 129 | [ 130 | { 131 | oldKey: "appURL", 132 | newKey: "url", 133 | }, 134 | { 135 | oldKey: "object1.bool", 136 | newKey: "object1.enabled", 137 | }, 138 | ] 139 | ); 140 | const expected = { 141 | url: "localhost", 142 | object1: { 143 | enabled: true, 144 | name: "app", 145 | }, 146 | }; 147 | 148 | if (isEqual(mapped, expected)) { 149 | t.pass(); 150 | } else { 151 | t.log("expected:"); 152 | t.log(JSON.stringify(expected, null, "\t")); 153 | t.log("got:"); 154 | t.log(JSON.stringify(mapped, null, "\t")); 155 | t.fail(); 156 | } 157 | }); 158 | 159 | test("change-value", (t) => { 160 | const mapped = _app.map( 161 | { 162 | appURL: "localhost", 163 | object1: { 164 | bool: true, 165 | name: "app", 166 | }, 167 | }, 168 | [ 169 | { 170 | oldKey: "object1.bool", 171 | newKey: "object1.enabled", 172 | values: [ 173 | { 174 | oldValue: true, 175 | newValue: "Y", 176 | }, 177 | ], 178 | }, 179 | ] 180 | ); 181 | const expected = { 182 | appURL: "localhost", 183 | object1: { 184 | enabled: "Y", 185 | name: "app", 186 | }, 187 | }; 188 | 189 | if (isEqual(mapped, expected)) { 190 | t.pass(); 191 | } else { 192 | t.log("expected:"); 193 | t.log(JSON.stringify(expected, null, "\t")); 194 | t.log("got:"); 195 | t.log(JSON.stringify(mapped, null, "\t")); 196 | t.fail(); 197 | } 198 | }); 199 | 200 | test("remove", (t) => { 201 | const mapped = _app.map( 202 | { 203 | appURL: "localhost", 204 | object1: { 205 | bool: true, 206 | name: "app", 207 | }, 208 | }, 209 | [ 210 | { 211 | oldKey: "appURL", 212 | }, 213 | { 214 | oldKey: "object1.bool", 215 | }, 216 | ] 217 | ); 218 | const expected = { 219 | object1: { 220 | name: "app", 221 | }, 222 | }; 223 | 224 | if (isEqual(mapped, expected)) { 225 | t.pass(); 226 | } else { 227 | t.log("expected:"); 228 | t.log(JSON.stringify(expected, null, "\t")); 229 | t.log("got:"); 230 | t.log(JSON.stringify(mapped, null, "\t")); 231 | t.fail(); 232 | } 233 | }); 234 | 235 | test("dependsOn", (t) => { 236 | const mapped = _app.map( 237 | { 238 | appURL: "localhost", 239 | object1: { 240 | bool: true, 241 | name: "app", 242 | }, 243 | }, 244 | [ 245 | { 246 | newKey: "object2", 247 | dependsOn: { 248 | key: "object1.bool", 249 | if: true, 250 | ifValue: "something", 251 | elseValue: "something else", 252 | }, 253 | }, 254 | ] 255 | ); 256 | const expected = { 257 | appURL: "localhost", 258 | object1: { 259 | bool: true, 260 | name: "app", 261 | }, 262 | object2: "something", 263 | }; 264 | 265 | if (isEqual(mapped, expected)) { 266 | t.pass(); 267 | } else { 268 | t.log("expected:"); 269 | t.log(JSON.stringify(expected, null, "\t")); 270 | t.log("got:"); 271 | t.log(JSON.stringify(mapped, null, "\t")); 272 | t.fail(); 273 | } 274 | }); 275 | 276 | test("newVal", (t) => { 277 | const mapped = _app.map( 278 | { 279 | appURL: "localhost", 280 | object1: { 281 | bool: true, 282 | name: "app", 283 | }, 284 | }, 285 | [ 286 | { 287 | newKey: "object1.test", 288 | dependsOn: { 289 | key: "object1.test", 290 | if: undefined, 291 | ifValue: true, 292 | }, 293 | }, 294 | ] 295 | ); 296 | const expected = { 297 | appURL: "localhost", 298 | object1: { 299 | bool: true, 300 | name: "app", 301 | test: true, 302 | }, 303 | }; 304 | 305 | if (isEqual(mapped, expected)) { 306 | t.pass(); 307 | } else { 308 | t.log("expected:"); 309 | t.log(JSON.stringify(expected, null, "\t")); 310 | t.log("got:"); 311 | t.log(JSON.stringify(mapped, null, "\t")); 312 | t.fail(); 313 | } 314 | }); 315 | 316 | test("newValExist", (t) => { 317 | const mapped = _app.map( 318 | { 319 | appURL: "localhost", 320 | object1: { 321 | bool: true, 322 | name: "app", 323 | test: true, 324 | }, 325 | }, 326 | [ 327 | { 328 | newKey: "object1.test", 329 | dependsOn: { 330 | key: "object1.test", 331 | if: undefined, 332 | ifValue: true, 333 | }, 334 | }, 335 | ] 336 | ); 337 | const expected = { 338 | appURL: "localhost", 339 | object1: { 340 | bool: true, 341 | name: "app", 342 | test: true, 343 | }, 344 | }; 345 | 346 | if (isEqual(mapped, expected)) { 347 | t.pass(); 348 | } else { 349 | t.log("expected:"); 350 | t.log(JSON.stringify(expected, null, "\t")); 351 | t.log("got:"); 352 | t.log(JSON.stringify(mapped, null, "\t")); 353 | t.fail(); 354 | } 355 | }); 356 | 357 | test("newValExist2", (t) => { 358 | const mapped = _app.map( 359 | { 360 | appURL: "localhost", 361 | object1: { 362 | bool: true, 363 | name: "app", 364 | test: false, 365 | }, 366 | }, 367 | [ 368 | { 369 | newKey: "object1.test", 370 | dependsOn: { 371 | key: "object1.test", 372 | if: undefined, 373 | ifValue: true, 374 | }, 375 | }, 376 | ] 377 | ); 378 | const expected = { 379 | appURL: "localhost", 380 | object1: { 381 | bool: true, 382 | name: "app", 383 | test: false, 384 | }, 385 | }; 386 | 387 | if (isEqual(mapped, expected)) { 388 | t.pass(); 389 | } else { 390 | t.log("expected:"); 391 | t.log(JSON.stringify(expected, null, "\t")); 392 | t.log("got:"); 393 | t.log(JSON.stringify(mapped, null, "\t")); 394 | t.fail(); 395 | } 396 | }); 397 | 398 | test("oldEqualNew", (t) => { 399 | const mapped = _app.map( 400 | { 401 | appURL: "localhost", 402 | object1: { 403 | bool: true, 404 | name: "app", 405 | }, 406 | }, 407 | [ 408 | { 409 | oldKey: "object1.name", 410 | newKey: "object1.name", 411 | values: [ 412 | { 413 | oldValue: "app", 414 | newValue: "app2", 415 | }, 416 | ], 417 | }, 418 | ] 419 | ); 420 | const expected = { 421 | appURL: "localhost", 422 | object1: { 423 | bool: true, 424 | name: "app2", 425 | }, 426 | }; 427 | 428 | if (isEqual(mapped, expected)) { 429 | t.pass(); 430 | } else { 431 | t.log("expected:"); 432 | t.log(JSON.stringify(expected, null, "\t")); 433 | t.log("got:"); 434 | t.log(JSON.stringify(mapped, null, "\t")); 435 | t.fail(); 436 | } 437 | }); 438 | --------------------------------------------------------------------------------