Last active
December 19, 2017 01:56
-
-
Save ChrisCinelli/60b465b7a21856106098927cac5f1bdf to your computer and use it in GitHub Desktop.
Fast string template with tolerance to undefined
This file contains 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 characters
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 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 characters
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 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 characters
// 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"`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment