Last active
December 19, 2017 01:56
-
-
Save ChrisCinelli/60b465b7a21856106098927cac5f1bdf to your computer and use it in GitHub Desktop.
Revisions
-
ChrisCinelli revised this gist
Dec 19, 2017 . 3 changed files with 58 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,9 @@ const _get = require('lodash/get'); const regEx = /\{\s*([a-zA-Z0-9\[\]\._]+)(\s*\|\s*['"]([^\}\s]+)['"])?\s*\}/g; /* f({a: 1, b: { c: 2, d: 3}}, '({a}, {b.c}, {c.d})') will output (1, 2, 3) */ module.exports = (obj, str) => str.replace(regEx, (match, p1, p2, p3) => _get(obj, p1) || p3 || ''); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ var chai = require('chai'); var expect = chai.expect; var _t = require('index.js'); var data = { address: { street: [ "10E Hamilton Ave", "Ste 500" ], city: "Campbell", stateOrProvince: "CA", postalCode: "95008", } } // testUtils.enableSnapshots(); describe('utils/template', function() { it('Should return the right address', function() { const str = _t(data, '{address.street[0]} {address.street[1]}, {address.city}, {address.stateOrProvince} {address.postalCode}'); chai.expect(str).to.matchSnapshot(); }); it('Should return the right address when a property is missing', function() { const str = _t(data, '{address.street[0]} {address.street[1]} {address.street[2]}, {address.city}, {address.stateOrProvince} {address.postalCode}'); chai.expect(str).to.matchSnapshot(); }); it('Should be able to work with missing closed curly bracket', function() { const str = _t(data, '{address.street[0]} {address.street[1]}, {address.city}, {address.stateOrProvince} {address.postalCode'); chai.expect(str).to.matchSnapshot(); }); it('Should be able to use default', function() { const str = _t(data, '{address.city} {address.noHere | "alternate" }'); chai.expect(str).to.matchSnapshot(); }); it('Default needs quotes', function() { const str = _t(data, '{address.city} {address.noHere | alternate }'); chai.expect(str).to.matchSnapshot(); }); }); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`utils/template Default needs quotes 1`] = `"Campbell {address.noHere | alternate }"`; exports[`utils/template Should be able to use default 1`] = `"Campbell alternate"`; exports[`utils/template Should be able to work with missing closed curly bracket 1`] = `"10E Hamilton Ave Ste 500, Campbell, CA {address.postalCode"`; exports[`utils/template Should return the right address 1`] = `"10E Hamilton Ave Ste 500, Campbell, CA 95008"`; exports[`utils/template Should return the right address when a property is missing 1`] = `"10E Hamilton Ave Ste 500 , Campbell, CA 95008"`; -
ChrisCinelli created this gist
Dec 19, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ const _get = require('lodash/get'); const regEx = /\{([a-zA-Z0-9\._]+)\}/g; /* f({a: 1, b: { c: 2, d: 3}}, '({a}, {b.c}, {c.d})') will output (1, 2, 3) */ module.exports = (obj, str) => str.replace(regEx, (match, p1) => _get(obj, p1) || '');