Skip to content

Instantly share code, notes, and snippets.

@ChrisCinelli
Last active December 19, 2017 01:56
Show Gist options
  • Select an option

  • Save ChrisCinelli/60b465b7a21856106098927cac5f1bdf to your computer and use it in GitHub Desktop.

Select an option

Save ChrisCinelli/60b465b7a21856106098927cac5f1bdf to your computer and use it in GitHub Desktop.

Revisions

  1. ChrisCinelli revised this gist Dec 19, 2017. 3 changed files with 58 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,9 @@
    const _get = require('lodash/get');
    const regEx = /\{([a-zA-Z0-9\._]+)\}/g;
    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)
    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) || '');
    module.exports = (obj, str) => str.replace(regEx, (match, p1, p2, p3) => _get(obj, p1) || p3 || '');
    44 changes: 44 additions & 0 deletions index.test.js
    Original 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();
    });
    });
    11 changes: 11 additions & 0 deletions index.test.js.snap
    Original 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"`;
  2. ChrisCinelli created this gist Dec 19, 2017.
    11 changes: 11 additions & 0 deletions index.js
    Original 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) || '');