Comparing version 0.1.3 to 0.1.4
15
mexna.js
'use strict'; | ||
var _ = require('lodash'); | ||
var includes = _.includes; | ||
var isEmpty = _.isEmpty; | ||
@@ -35,12 +36,8 @@ | ||
var key = expression; | ||
var hasDefaultValue = false; | ||
var defaultValue = null; | ||
var value = null; | ||
var keys = options.keys; | ||
var useDefaultValue = false; | ||
var keyValue; | ||
if (_.contains(expression, options.delimeter)) { | ||
hasDefaultValue = true; | ||
if (includes(expression, options.delimeter)) { | ||
var parts = expression.split(options.delimeter); | ||
@@ -55,3 +52,2 @@ if (parts.length !== 2) { | ||
if (options.keys[key] === undefined) { | ||
useDefaultValue = true; | ||
try { | ||
@@ -70,2 +66,7 @@ defaultValue = JSON.parse(defaultValueExpression); | ||
if (options.strict && !options.keys.hasOwnProperty(key) && !defaultValue) { | ||
throw new Error('Range Error: `' + key + '` is not defined. ' | ||
+ 'Consider adding it to the `keys` option or turning off the strict mode.'); | ||
} | ||
keyValue = keys[key]; | ||
@@ -102,3 +103,3 @@ value = keyValue === '' ? keyValue : (keyValue || defaultValue || ''); | ||
* @param {string} match | ||
* @param {key} match | ||
* @param {key} key | ||
* @return {string} | ||
@@ -105,0 +106,0 @@ */ |
{ | ||
"name": "mexna", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Yet another interpolator", | ||
"main": "mexna.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/gulp test", | ||
"test": "mocha --reporter spec --require must-dist --timeout 3000 test/*.test.js", | ||
"postinstall": "./tasks/post-install" | ||
@@ -15,10 +15,9 @@ }, | ||
"dependencies": { | ||
"lodash": "3.10.0" | ||
"lodash": "4.17.4" | ||
}, | ||
"devDependencies": { | ||
"gulp": "^3.9.0", | ||
"gulp-mocha": "^2.2.0", | ||
"jscs": "2.0.0", | ||
"jshint": "2.7.0", | ||
"jshint-stylish": "0.4.0", | ||
"mocha": "^3.2.0", | ||
"must-dist": "^0.12.0" | ||
@@ -25,0 +24,0 @@ }, |
@@ -1,5 +0,5 @@ | ||
# mexna | ||
# mexna [![Build Status](https://travis-ci.org/zefirka/mexna.svg?branch=master)](https://travis-ci.org/zefirka/mexna) | ||
Mexna is string interpolator with simple signature but pretty specific abilities. | ||
Main syntax is: `mexna(string, [options])`. | ||
Mexna is string interpolator with simple signature but pretty specific abilities. | ||
Main syntax is: `mexna(string, [options])`. | ||
Mexna can: | ||
@@ -26,5 +26,7 @@ 1. interpolate into string data in case of simple object with keys to interpolate, | ||
- `regex` - regular expression to match. Must have **one group** containing `key` and optionaly `default` values for keys. | ||
- `strict` - throws exceptions for unknown keys in the strict mode. Defaults `false`. | ||
## Usage | ||
## Usage | ||
#### Simple string interpolation | ||
@@ -62,3 +64,3 @@ You can give your own regex to match interpolants with `regex` option. By default it equals to `/${(.+?)}/g`, which matches to expressions kinda `${thisIs}`: | ||
``` | ||
or just put empty `keys` and use default values for interpolants: | ||
@@ -65,0 +67,0 @@ |
@@ -6,3 +6,3 @@ 'use strict'; | ||
describe('main functionality', () => { | ||
describe('interpolation of strings:', () => { | ||
@@ -74,8 +74,7 @@ const simpleString = 'I am a simple ${what}'; | ||
mexna(multipleStringWithDefault).must.to.be('I am a multiple first and second'); | ||
}); | ||
}); | ||
}) | ||
describe('interpolation of types', () => { | ||
const strArr = 'I am ${array || [1,2,3]}'; | ||
const strBool = 'I am ${boolean || false}'; | ||
const strArrOut = '${array || [1, 2, 3]}'; | ||
@@ -136,2 +135,18 @@ | ||
}); | ||
describe('strict mode', () => { | ||
it('should throw an exception for the undefined key', () => { | ||
(() => mexna('${missing_key}', { | ||
keys: {}, | ||
strict: true | ||
})).must.throw(Error, /^Range Error: `missing_key`/); | ||
}); | ||
it('shouldn\'t throw an exception for the undefined key with default value', () => { | ||
mexna('${missing_key || "yo"}', { | ||
keys: {}, | ||
strict: true | ||
}).must.to.be('yo'); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
80899
5
137
15
297
+ Addedlodash@4.17.4(transitive)
- Removedlodash@3.10.0(transitive)
Updatedlodash@4.17.4