message-box
Advanced tools
Comparing version 0.0.2 to 0.1.0
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
exports.SUGGESTED_EVALUATE = exports.DEFAULT_ESCAPE = exports.DEFAULT_INTERPOLATE = undefined; | ||
@@ -14,9 +15,9 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _handlebars = require('handlebars'); | ||
var _lodash = require('lodash.defaultsdeep'); | ||
var _handlebars2 = _interopRequireDefault(_handlebars); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _deepExtend = require('deep-extend'); | ||
var _lodash3 = require('lodash.template'); | ||
var _deepExtend2 = _interopRequireDefault(_deepExtend); | ||
var _lodash4 = _interopRequireDefault(_lodash3); | ||
@@ -27,2 +28,10 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// Default lodash templates regexs | ||
// https://regex101.com/r/ce27tA/5 | ||
var DEFAULT_INTERPOLATE = exports.DEFAULT_INTERPOLATE = /{{{([^\{\}#][\s\S]+?)}}}/g; | ||
// https://regex101.com/r/8sRC8b/8 | ||
var DEFAULT_ESCAPE = exports.DEFAULT_ESCAPE = /{{([^\{\}#][\s\S]+?)}}/g; | ||
// https://regex101.com/r/ndDqxg/4 | ||
var SUGGESTED_EVALUATE = exports.SUGGESTED_EVALUATE = /{{#([^\{\}].*?)}}/g; | ||
var MessageBox = function () { | ||
@@ -35,2 +44,5 @@ function MessageBox() { | ||
var tracker = _ref.tracker; | ||
var interpolate = _ref.interpolate; | ||
var evaluate = _ref.evaluate; | ||
var escape = _ref.escape; | ||
@@ -42,2 +54,7 @@ _classCallCheck(this, MessageBox); | ||
if (tracker) this.trackerDep = new tracker.Dependency(); | ||
// Template options | ||
this.interpolate = interpolate || MessageBox.interpolate || DEFAULT_INTERPOLATE; | ||
this.evaluate = evaluate || MessageBox.evaluate; | ||
this.escape = escape || MessageBox.escape || DEFAULT_ESCAPE; | ||
} | ||
@@ -48,3 +65,3 @@ | ||
value: function messages(_messages) { | ||
(0, _deepExtend2.default)(this.messageList, _messages); | ||
(0, _lodash2.default)(this.messageList, _messages); | ||
} | ||
@@ -63,3 +80,3 @@ }, { | ||
if (messages) { | ||
if (globalMessages) messages = (0, _deepExtend2.default)({}, globalMessages, messages); | ||
if (globalMessages) messages = (0, _lodash2.default)({}, globalMessages, messages); | ||
} else { | ||
@@ -103,3 +120,9 @@ messages = globalMessages; | ||
if (typeof message === 'string') message = _handlebars2.default.compile(message); | ||
if (typeof message === 'string') { | ||
message = (0, _lodash4.default)(message, { | ||
interpolate: this.interpolate, | ||
evaluate: this.evaluate, | ||
escape: this.escape | ||
}); | ||
} | ||
@@ -129,8 +152,15 @@ if (typeof message !== 'function') return fieldName + ' is invalid'; | ||
var messages = _ref3.messages; | ||
var interpolate = _ref3.interpolate; | ||
var evaluate = _ref3.evaluate; | ||
var escape = _ref3.escape; | ||
if (typeof initialLanguage === 'string') MessageBox.language = initialLanguage; | ||
if (interpolate instanceof RegExp) MessageBox.interpolate = interpolate; | ||
if (evaluate instanceof RegExp) MessageBox.evaluate = evaluate; | ||
if (escape instanceof RegExp) MessageBox.escape = escape; | ||
if (messages) { | ||
if (!MessageBox.messages) MessageBox.messages = {}; | ||
(0, _deepExtend2.default)(MessageBox.messages, messages); | ||
(0, _lodash2.default)(MessageBox.messages, messages); | ||
} | ||
@@ -137,0 +167,0 @@ } |
{ | ||
"name": "message-box", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "A package for defining and getting validation error messages, with support for Meteor Tracker reactivity", | ||
@@ -30,4 +30,4 @@ "author": "Eric Dobbertin <aldeed@gmail.com>", | ||
"dependencies": { | ||
"deep-extend": "^0.4.1", | ||
"handlebars": "^4.0.5" | ||
"lodash.defaultsdeep": "^4.6.0", | ||
"lodash.template": "^4.4.0" | ||
}, | ||
@@ -34,0 +34,0 @@ "devDependencies": { |
# MessageBox | ||
A package for defining and getting validation error messages. Meteor apps can pass in `Tracker` to get reactive language selection. | ||
An NPM package for defining and getting validation error messages in JavaScript. Meteor apps can pass in `Tracker` to get reactive language selection. | ||
## Installation | ||
```bash | ||
$ npm i --save message-box | ||
``` | ||
## Usage | ||
@@ -46,3 +52,3 @@ | ||
```js | ||
messageBox.addMessages({ | ||
messageBox.messages({ | ||
en: { | ||
@@ -58,3 +64,3 @@ errorType: '{{name}} is invalid' || function, | ||
`addMessages` does a deep extend on the existing messages. | ||
`messages` does a deep extend on the existing messages. | ||
@@ -166,1 +172,30 @@ For any of the ways you can set messages, the message can be either a string or a function that returns a string. If it's a string, it may contain handlebars placeholders for anything in the error object or anything passed in the `context` option of the `message` function. If it's a function, it will receive a single `context` argument that has all the same properties that are available as handlebars variables. | ||
Then when you change the language, any call to `messageBox.message()` that does not specify a language and is in a reactive context will rerun. | ||
### Template | ||
By default (and historically) the substitution of strings is made using `{{}}`, you can change this by passing the `interpolate` and` escape` options: | ||
```js | ||
const messageBox = new MessageBox({ | ||
messages: { ... }, | ||
interpolate: /{{{([^\{\}#][\s\S]+?)}}}/g, // default | ||
escape: /{{([^\{\}#][\s\S]+?)}}/g; // default | ||
}); | ||
``` | ||
It is also possible (but I would not recommend) to use logic within messages by using the `evaluate` option: | ||
```js | ||
var SUGGESTED_EVALUATE = require('MessageBox').SUGGESTED_EVALUATE | ||
// or | ||
import { SUGGESTED_EVALUATE } from 'MessageBox'; | ||
const messageBox = new MessageBox({ | ||
messages: { | ||
en: { | ||
conditional: '{{# if (value) { }}true{{# } else { }}false{{# } }}', | ||
} | ||
}, | ||
evaluate: SUGGESTED_EVALUATE, | ||
}); | ||
``` |
Sorry, the diff of this file is not supported yet
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
14388
127
199
+ Addedlodash.defaultsdeep@^4.6.0
+ Addedlodash.template@^4.4.0
+ Addedlodash._reinterpolate@3.0.0(transitive)
+ Addedlodash.defaultsdeep@4.6.1(transitive)
+ Addedlodash.template@4.5.0(transitive)
+ Addedlodash.templatesettings@4.2.0(transitive)
- Removeddeep-extend@^0.4.1
- Removedhandlebars@^4.0.5
- Removeddeep-extend@0.4.2(transitive)
- Removedhandlebars@4.7.8(transitive)
- Removedminimist@1.2.8(transitive)
- Removedneo-async@2.6.2(transitive)
- Removedsource-map@0.6.1(transitive)
- Removeduglify-js@3.19.3(transitive)
- Removedwordwrap@1.0.0(transitive)