format-message
Advanced tools
Comparing version 5.0.0-beta.1 to 5.0.0-beta.2
@@ -9,2 +9,3 @@ 'use strict' | ||
module.exports = function formatChildren (applyChildren, message, wrappers) { | ||
if (!wrappers) return message | ||
var mm = message.length | ||
@@ -15,6 +16,2 @@ var stack = [] | ||
var curlyDepth = 0 | ||
var keys = Object.keys(wrappers || {}).sort(function (a, b) { | ||
return b.length - a.length // longest first | ||
}) | ||
var kk = keys.length | ||
var last = 0 | ||
@@ -24,39 +21,51 @@ for (var m = 0; m < mm; ++m) { | ||
if (message[m] === '}') --curlyDepth | ||
if (curlyDepth % 2 === 1) continue | ||
if (message[m] !== '<' || curlyDepth % 2 === 1) continue | ||
for (var k = 0; k < kk; ++k) { | ||
var key = keys[k] | ||
if (message.slice(m, m + key.length) === key) { | ||
if (currentKey === key) { // end token | ||
var end = m | ||
if (message.slice(m - 1, m) === ' ') { | ||
--end // skip trailing space inside tag | ||
} | ||
if (last < end) { | ||
current.push(message.slice(last, end)) | ||
} | ||
var children = current | ||
current = stack.pop() | ||
currentKey = stack.pop() | ||
current.push(applyChildren(wrappers[key], children)) | ||
} else { // start token | ||
if (last < m) { | ||
current.push(message.slice(last, m)) | ||
} | ||
stack.push(currentKey) | ||
stack.push(current) | ||
currentKey = key | ||
current = [] | ||
if (message.slice(m + key.length, m + key.length + 1) === ' ') { | ||
++m // skip leading space inside tag | ||
} | ||
} | ||
last = m + key.length | ||
m = last - 1 // offset next ++ | ||
break | ||
var isSelfClosing = false | ||
var isEnd = false | ||
var s = m + 1 // skip < | ||
if (message[s] === '/') { | ||
isEnd = true | ||
++s | ||
} | ||
var e = s | ||
while (message[e] >= '0' && message[e] <= '9') { ++e } | ||
if (!isEnd && message.slice(e, e + 2) === '/>') { | ||
isSelfClosing = true | ||
} else if (message[e] !== '>') { | ||
continue | ||
} | ||
var key = +message.slice(s, e) | ||
if (!wrappers[key]) continue | ||
++e | ||
if (isSelfClosing) ++e | ||
if (last < m) { | ||
current.push(message.slice(last, m)) | ||
} | ||
if (isSelfClosing) { | ||
current.push(applyChildren(wrappers[key], null)) | ||
} else if (isEnd) { | ||
if (currentKey !== key) { | ||
throw new Error('Wrapping tags not properly nested in "' + message + '"') | ||
} | ||
var children = current | ||
current = stack.pop() | ||
currentKey = stack.pop() | ||
current.push(applyChildren(wrappers[key], children)) | ||
} else { // start token | ||
stack.push(currentKey) | ||
stack.push(current) | ||
currentKey = key | ||
current = [] | ||
} | ||
last = e | ||
m = e - 1 // offset next ++ | ||
} | ||
if (stack.length > 0) { | ||
throw new Error('Wrapping tokens not properly nested in "' + message + '"') | ||
throw new Error('Wrapping tags not properly nested in "' + message + '"') | ||
} | ||
@@ -63,0 +72,0 @@ if (last < m) { |
@@ -9,5 +9,8 @@ 'use strict' | ||
} | ||
return element.setChildren(children.length === 1 ? children[0] : children) | ||
if (children) { | ||
element.setChildren(children.length === 1 ? children[0] : children) | ||
} | ||
return element | ||
} | ||
exports.formatChildren = formatChildren.bind(null, applyChildren) |
{ | ||
"name": "format-message", | ||
"version": "5.0.0-beta.1", | ||
"version": "5.0.0-beta.2", | ||
"description": "Internationalize text, numbers, and dates using ICU Message Format", | ||
@@ -5,0 +5,0 @@ "author": "Andy VanWagoner <thetalecrafter@gmail.com> (https://thetalecrafter.com/)", |
@@ -10,5 +10,5 @@ 'use strict' | ||
} | ||
return React.cloneElement.apply(React, [ element, null ].concat(children)) | ||
return React.cloneElement.apply(React, [ element, null ].concat(children || [])) | ||
} | ||
exports.formatChildren = formatChildren.bind(null, applyChildren) |
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
28407
214