Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from trustpilot/updates
Browse files Browse the repository at this point in the history
Preparing next release
  • Loading branch information
Martin Andersen authored Sep 20, 2017
2 parents 127f45b + afb0343 commit a16dc7a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 53 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ function getTokenValue(tokens, tokenName, delimiter) {
function replace(target, options) {
options = injectDefaultOptions(options);

var includeRegExp = new RegExp(escapeRegExp(options.prefix) + '(.+?)' + escapeRegExp(options.suffix), 'g');
var regexPattern = `${ escapeRegExp(options.prefix) }(.+?)${ escapeRegExp(options.suffix)}`;
var includeRegExp = new RegExp(regexPattern, 'g');
var isObject = false;
var text;

if (typeof target === 'object') {
text = JSON.stringify(target);
isObject = true;
} else if ( typeof target === 'string') {
text = target;
text = target;
} else {
text = target.toString();
}
Expand All @@ -73,12 +74,12 @@ function replace(target, options) {
if (tokenValue !== null) {
if (typeof tokenValue === 'object') {
tokenValue = JSON.stringify(tokenValue);
}
if (typeof tokenValue === 'string') {
if(tokenValue.indexOf('"') > -1){
} else if (typeof tokenValue === 'string') {
if (tokenValue.indexOf('"') > -1) {
tokenValue = tokenValue.replace(/"/g, '\\"');
}
}

retVal = retVal.replace(fullMatch, tokenValue);
}
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "token-substitute",
"version": "1.1.1",
"version": "1.2.0",
"description": "Substitute tokens in an object",
"homepage": "https://github.com/trustpilot/node-token-substitute",
"repository": "https://github.com/trustpilot/node-token-substitute.git",
Expand All @@ -13,10 +13,10 @@
"devDependencies": {
"babel-eslint": "7.2.1",
"chai": "3.5.0",
"eslint": "3.18.0",
"eslint-config-trustpilot": "1.7.0",
"eslint": "3.19.0",
"eslint-config-trustpilot": "1.8.0",
"eslint-plugin-babel": "4.1.1",
"eslint-plugin-react": "6.10.3",
"mocha": "3.2.0"
"mocha": "3.5.2"
}
}
18 changes: 12 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ describe('Default option value tests', () => {
test3: '#{key3-1}',
test4: '#{key4.1}',
test5: {
test1: '#{key5.1}',
test2: '#{key5.2}'
test51: '#{key5.1}'
},
test6: '#{key1}'
test6: '#{key6}'
};

it('Test default options', () => {
Expand All @@ -22,15 +21,15 @@ describe('Default option value tests', () => {
assert.notStrictEqual(actual, config);
assert.equal(actual.test1, '');
assert.equal(actual.test2, '{{key2}}');
assert.equal(actual.test5.test1, '');
assert.equal(actual.test5.test51, '');
});

it('Test preserveUnknownTokens set to true', () => {
const options = { preserveUnknownTokens: true };
const actual = substitute(config, options);
assert.notEqual(actual.test1, '', 'Expected to preserve value');
assert.equal(actual.test2, '{{key2}}');
assert.notEqual(actual.test5.test1, '');
assert.notEqual(actual.test5.test51, '');
});

it('Test tokens is set', () => {
Expand All @@ -57,7 +56,7 @@ describe('Default option value tests', () => {
assert.equal(actual.test1, 'value1');
assert.equal(actual.test3, 'value3.1');
assert.equal(actual.test4, 'value4.1');
assert.equal(actual.test5.test1, 'value5.1');
assert.equal(actual.test5.test51, 'value5.1');
});

it('Testing that the target can be string value', () => {
Expand All @@ -73,4 +72,11 @@ describe('Default option value tests', () => {
assert.include(actual, 'value5.1');
assert.notInclude(actual, '#{key1}');
});

it('Test that value can contain quotes', () => {
const options = {configFile: './test/test.json' };
const configvalue = JSON.stringify(config);
const actual = substitute(configvalue, options);
assert.include(actual, 'value \\" with \\" quotes');
});
});
5 changes: 2 additions & 3 deletions test/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"key1": "value1",
"key2": "value2",
"key3-1": "value3.1",
"key3-2": "value3.2",
"key4.1": "value4.1",
"key4.2": "value4.2",
"key5.1": "value5.1"
"key5.1": "value5.1",
"key6": "value \" with \" quotes"
}
64 changes: 29 additions & 35 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ d@1:
dependencies:
es5-ext "^0.10.9"

debug@2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.2.0.tgz#f87057e995b1a1f6ae6a4960664137bc56f039da"
debug@2.6.8:
version "2.6.8"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
dependencies:
ms "0.7.1"
ms "2.0.0"

debug@^2.1.1, debug@^2.2.0:
version "2.6.3"
Expand Down Expand Up @@ -269,9 +269,9 @@ del@^2.0.2:
pinkie-promise "^2.0.0"
rimraf "^2.2.8"

diff@1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-1.4.0.tgz#7f28d2eb9ee7b15a97efd89ce63dcfdaa3ccbabf"
diff@3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.2.0.tgz#c9ce393a4b7cbd0b058a725c93df299027868ff9"

doctrine@^1.2.2:
version "1.5.0"
Expand Down Expand Up @@ -369,9 +369,9 @@ escope@^3.6.0:
esrecurse "^4.1.0"
estraverse "^4.1.1"

eslint-config-trustpilot@1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/eslint-config-trustpilot/-/eslint-config-trustpilot-1.7.0.tgz#d237ca22a827eac8b2e8d5abe110b09ae3e49ada"
eslint-config-trustpilot@1.8.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/eslint-config-trustpilot/-/eslint-config-trustpilot-1.8.0.tgz#cce92f5eb143324da040ef46c6494b1927587793"

[email protected]:
version "4.1.1"
Expand All @@ -387,9 +387,9 @@ [email protected]:
jsx-ast-utils "^1.3.4"
object.assign "^4.0.4"

eslint@3.18.0:
version "3.18.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.18.0.tgz#647e985c4ae71502d20ac62c109f66d5104c8a4b"
eslint@3.19.0:
version "3.19.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.19.0.tgz#c8fc6201c7f40dd08941b87c085767386a679acc"
dependencies:
babel-code-frame "^6.16.0"
chalk "^1.1.3"
Expand Down Expand Up @@ -523,18 +523,7 @@ generate-object-property@^1.1.0:
dependencies:
is-property "^1.0.0"

[email protected]:
version "7.0.5"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.0.5.tgz#b4202a69099bbb4d292a7c1b95b6682b67ebdc95"
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.2"
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^7.0.0, glob@^7.0.3, glob@^7.0.5:
[email protected], glob@^7.0.0, glob@^7.0.3, glob@^7.0.5:
version "7.1.1"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.1.tgz#805211df04faaf1c63a3600306cdf5ade50b2ec8"
dependencies:
Expand Down Expand Up @@ -588,6 +577,10 @@ has@^1.0.1:
dependencies:
function-bind "^1.0.2"

[email protected]:
version "1.1.1"
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"

ignore@^3.2.0:
version "3.2.6"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.2.6.tgz#26e8da0644be0bb4cb39516f6c79f0e0f4ffe48c"
Expand Down Expand Up @@ -817,30 +810,31 @@ [email protected], mkdirp@^0.5.0, mkdirp@^0.5.1:
dependencies:
minimist "0.0.8"

mocha@3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-3.2.0.tgz#7dc4f45e5088075171a68896814e6ae9eb7a85e3"
mocha@3.5.2:
version "3.5.2"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-3.5.2.tgz#3d5585cc6bd9e43a7c0bd251631abc0fbea0f032"
dependencies:
browser-stdout "1.3.0"
commander "2.9.0"
debug "2.2.0"
diff "1.4.0"
debug "2.6.8"
diff "3.2.0"
escape-string-regexp "1.0.5"
glob "7.0.5"
glob "7.1.1"
growl "1.9.2"
he "1.1.1"
json3 "3.3.2"
lodash.create "3.1.1"
mkdirp "0.5.1"
supports-color "3.1.2"

[email protected]:
version "0.7.1"
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.1.tgz#9cd13c03adbff25b65effde7ce864ee952017098"

[email protected]:
version "0.7.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765"

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

[email protected]:
version "0.0.5"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.5.tgz#8fbfabb0a98a253d3184331f9e8deb7372fac6c0"
Expand Down

0 comments on commit a16dc7a

Please sign in to comment.