Comparing version
192
dist/hl7.js
@@ -1,2 +0,192 @@ | ||
var r=require("dset"),e=/\r\n|\r|\n/g,n=[[/\\F\\/g,"|"],[/\\S\\/g,"^"],[/\\T\\/g,"&"],[/\\E\\/g,"\\"]],t=[/(?:[^EFSTR])\\(?:[^EFSTR])/g,"\\E\\"],i=[[/\|/g,"\\F\\"],[/\^/g,"\\S\\"],[/\&/g,"\\T\\"]],c=["|","^","&"],u=/\d+/g,o=function(e){var n=Object.keys(e),o=n[0].split(".")[0],s="MSH"===o,a=[];n.forEach(function(n){var t=e[n],i=n.split(".");return r.dset(a,i.slice(1).join(".").replace(u,function(r){return Number(r)-1}),t),a});var f=function r(e,n){void 0===n&&(n=0);for(var t="",u=!1,o=0,a=e.length;o<a;o++){var f=c[n],l=e[o]||"";"object"==typeof l?(u=!0,l=r(l,n+1)):!s||0!==n||0!==o&&1!==o?u||(l=i.reduce(function(r,e){return r.replace(e[0],e[1])},l)):(l="",f=""),t+=(0===o?"":f)+l}return t}(a).replace(t[0],function(r){return r.slice(0,1)+t[1]+r.slice(-1)});return o+(s?"|^~\\&":"|")+f};exports.endOfLineRe=e,exports.getMessages=function(r){for(var e=[],n=r.indexOf("MSH|^");-1!==n;){var t=n||0;n=r.indexOf("MSH|^",n+1),e.push(-1!==n?r.slice(t,n).trim():r.slice(t).trim())}return e},exports.parse=function(r){return r.trim().split(e).map(function(r){return e=r.trim(),t={},function r(e,i,u){var o=e.split(u),s=c[c.indexOf(u)+1],a="MSH"===i;o.forEach(function(e,c){var o=i+"."+(c+(a&&"|"===u?2:1));"MSH.2"===o?t[o]=e:s&&e.includes(s)?r(e,o,s):t[o]=e?n.reduce(function(r,e){return r.replace(e[0],e[1])},e):""})}((i=e.split("|")).slice(1).join("|"),i[0],"|"),t;var e,t,i})},exports.serialize=function(r){return r.map(o).join("\r")}; | ||
/* | ||
dset inlined here, including license as per license in dset. | ||
The MIT License (MIT) | ||
Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (lukeed.com) | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
var dset = function dset(obj, keys, val) { | ||
keys.split && (keys = keys.split('.')); | ||
var i = 0, | ||
l = keys.length, | ||
t = obj, | ||
x, | ||
k; | ||
for (; i < l;) { | ||
k = keys[i++]; | ||
if (k === '__proto__' || k === 'constructor' || k === 'prototype') continue; | ||
t = t[k] = i === l ? val : (x = t[k]) != null ? x : keys[i] * 0 !== 0 || !!~keys[i].indexOf('.') ? {} : []; | ||
} | ||
}; | ||
var endOfLineRe = /\r\n|\r|\n/g; | ||
var unescapePairs = [[/\\F\\/g, '|'], [/\\S\\/g, '^'], [/\\T\\/g, '&'], [/\\E\\/g, '\\'] // [/\\R\\/g, '~'] | ||
]; | ||
var escapeCharacterPair = [/(?:[^EFSTR])\\(?:[^EFSTR])/g, '\\E\\']; | ||
var escapePairs = [[/\|/g, '\\F\\'], [/\^/g, '\\S\\'], [/\&/g, '\\T\\'] // [/\~/g, '\\R\\'] | ||
]; | ||
var escape = function escape(string) { | ||
return escapePairs.reduce(function (result, _ref) { | ||
var regEx = _ref[0], | ||
replacement = _ref[1]; | ||
return result.replace(regEx, replacement); | ||
}, string); | ||
}; | ||
var unescape = function unescape(string) { | ||
return unescapePairs.reduce(function (result, _ref2) { | ||
var regEx = _ref2[0], | ||
replacement = _ref2[1]; | ||
return result.replace(regEx, replacement); | ||
}, string); | ||
}; | ||
var delimiters = ['|', '^', '&']; | ||
var parseSegment = function parseSegment(segmentRaw) { | ||
var segment = segmentRaw.trim(); | ||
var result = {}; | ||
var parseForDelimiter = function parseForDelimiter(subject, address, delimiter) { | ||
var parts = subject.split(delimiter); | ||
var nextDelimiter = delimiters[delimiters.indexOf(delimiter) + 1]; | ||
var isHeader = address === 'MSH'; | ||
parts.forEach(function (part, index) { | ||
var indexAddend = isHeader && delimiter === '|' ? 2 : 1; | ||
var currentAddress = address + '.' + (index + indexAddend); | ||
var isDelimiterField = currentAddress === 'MSH.2'; | ||
if (isDelimiterField) { | ||
result[currentAddress] = part; | ||
} else if (nextDelimiter && part.includes(nextDelimiter)) { | ||
parseForDelimiter(part, currentAddress, nextDelimiter); | ||
} else { | ||
if (part) { | ||
// const toAdd = part.includes('~') ? part.split('~') : part | ||
result[currentAddress] = unescape(part); | ||
} else { | ||
result[currentAddress] = ''; | ||
} | ||
} | ||
}); | ||
}; | ||
var split = segment.split('|'); | ||
parseForDelimiter(split.slice(1).join('|'), split[0], '|'); | ||
return result; | ||
}; | ||
var parse = function parse(message) { | ||
return message.trim().split(endOfLineRe).map(function (seg) { | ||
return parseSegment(seg); | ||
}); | ||
}; | ||
var digitRe = /\d+/g; | ||
var decrementEachValue = function decrementEachValue(str) { | ||
return str.replace(digitRe, function (item) { | ||
return Number(item) - 1; | ||
}); | ||
}; | ||
var serializeSegment = function serializeSegment(obj) { | ||
var keys = Object.keys(obj); | ||
var segmentId = keys[0].split('.')[0]; | ||
var isMessageHeader = segmentId === 'MSH'; | ||
var result = []; | ||
keys.forEach(function (key) { | ||
var value = obj[key]; | ||
var splitKey = key.split('.'); | ||
dset(result, decrementEachValue(splitKey.slice(1).join('.')), value); | ||
return result; | ||
}); | ||
var join = function join(item, depth) { | ||
if (depth === void 0) { | ||
depth = 0; | ||
} | ||
var result = ''; | ||
var entryHasChildren = false; | ||
for (var i = 0, l = item.length; i < l; i++) { | ||
var entry = item[i]; | ||
var activeDelimiter = delimiters[depth]; | ||
var toAdd = entry || ''; | ||
if (typeof toAdd === 'object') { | ||
entryHasChildren = true; | ||
toAdd = join(toAdd, depth + 1); | ||
} else { | ||
var isEncodingCharsSegment = isMessageHeader && depth === 0 && (i === 0 || i === 1); | ||
if (isEncodingCharsSegment) { | ||
toAdd = ''; | ||
activeDelimiter = ''; | ||
} else { | ||
// console.log('ENTRY HAS CHILDRE', entryHasChildren) | ||
if (!entryHasChildren) { | ||
toAdd = escape(toAdd); | ||
} | ||
} | ||
} | ||
result += (i === 0 ? '' : activeDelimiter) + toAdd; | ||
} | ||
return result; | ||
}; | ||
var joined = join(result).replace(escapeCharacterPair[0], function (match) { | ||
return match.slice(0, 1) + escapeCharacterPair[1] + match.slice(-1); | ||
}); | ||
return segmentId + (isMessageHeader ? '|^~\\&' : '|') + joined; | ||
}; | ||
var serialize = function serialize(arrayOfSegments) { | ||
return arrayOfSegments.map(serializeSegment).join('\r'); | ||
}; | ||
var messageHead = 'MSH|^'; | ||
var getMessages = function getMessages(messageString) { | ||
var messages = []; | ||
var idx = messageString.indexOf(messageHead); | ||
while (idx !== -1) { | ||
var startingIndex = idx || 0; | ||
idx = messageString.indexOf(messageHead, idx + 1); | ||
if (idx !== -1) { | ||
messages.push(messageString.slice(startingIndex, idx).trim()); | ||
} else { | ||
messages.push(messageString.slice(startingIndex).trim()); | ||
} | ||
} | ||
return messages; | ||
}; | ||
exports.endOfLineRe = endOfLineRe; | ||
exports.getMessages = getMessages; | ||
exports.parse = parse; | ||
exports.serialize = serialize; | ||
//# sourceMappingURL=hl7.js.map |
@@ -1,2 +0,189 @@ | ||
import{dset as r}from"dset";var n=/\r\n|\r|\n/g,e=[[/\\F\\/g,"|"],[/\\S\\/g,"^"],[/\\T\\/g,"&"],[/\\E\\/g,"\\"]],t=[/(?:[^EFSTR])\\(?:[^EFSTR])/g,"\\E\\"],i=[[/\|/g,"\\F\\"],[/\^/g,"\\S\\"],[/\&/g,"\\T\\"]],c=["|","^","&"],u=function(r){return r.trim().split(n).map(function(r){return n=r.trim(),t={},function r(n,i,u){var o=n.split(u),f=c[c.indexOf(u)+1],a="MSH"===i;o.forEach(function(n,c){var o=i+"."+(c+(a&&"|"===u?2:1));"MSH.2"===o?t[o]=n:f&&n.includes(f)?r(n,o,f):t[o]=n?e.reduce(function(r,n){return r.replace(n[0],n[1])},n):""})}((i=n.split("|")).slice(1).join("|"),i[0],"|"),t;var n,t,i})},o=/\d+/g,f=function(n){var e=Object.keys(n),u=e[0].split(".")[0],f="MSH"===u,a=[];e.forEach(function(e){var t=n[e],i=e.split(".");return r(a,i.slice(1).join(".").replace(o,function(r){return Number(r)-1}),t),a});var l=function r(n,e){void 0===e&&(e=0);for(var t="",u=!1,o=0,a=n.length;o<a;o++){var l=c[e],p=n[o]||"";"object"==typeof p?(u=!0,p=r(p,e+1)):!f||0!==e||0!==o&&1!==o?u||(p=i.reduce(function(r,n){return r.replace(n[0],n[1])},p)):(p="",l=""),t+=(0===o?"":l)+p}return t}(a).replace(t[0],function(r){return r.slice(0,1)+t[1]+r.slice(-1)});return u+(f?"|^~\\&":"|")+l},a=function(r){return r.map(f).join("\r")},l=function(r){for(var n=[],e=r.indexOf("MSH|^");-1!==e;){var t=e||0;e=r.indexOf("MSH|^",e+1),n.push(-1!==e?r.slice(t,e).trim():r.slice(t).trim())}return n};export{n as endOfLineRe,l as getMessages,u as parse,a as serialize}; | ||
/* | ||
dset inlined here, including license as per license in dset. | ||
The MIT License (MIT) | ||
Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (lukeed.com) | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
var dset = function dset(obj, keys, val) { | ||
keys.split && (keys = keys.split('.')); | ||
var i = 0, | ||
l = keys.length, | ||
t = obj, | ||
x, | ||
k; | ||
for (; i < l;) { | ||
k = keys[i++]; | ||
if (k === '__proto__' || k === 'constructor' || k === 'prototype') continue; | ||
t = t[k] = i === l ? val : (x = t[k]) != null ? x : keys[i] * 0 !== 0 || !!~keys[i].indexOf('.') ? {} : []; | ||
} | ||
}; | ||
var endOfLineRe = /\r\n|\r|\n/g; | ||
var unescapePairs = [[/\\F\\/g, '|'], [/\\S\\/g, '^'], [/\\T\\/g, '&'], [/\\E\\/g, '\\'] // [/\\R\\/g, '~'] | ||
]; | ||
var escapeCharacterPair = [/(?:[^EFSTR])\\(?:[^EFSTR])/g, '\\E\\']; | ||
var escapePairs = [[/\|/g, '\\F\\'], [/\^/g, '\\S\\'], [/\&/g, '\\T\\'] // [/\~/g, '\\R\\'] | ||
]; | ||
var escape = function escape(string) { | ||
return escapePairs.reduce(function (result, _ref) { | ||
var regEx = _ref[0], | ||
replacement = _ref[1]; | ||
return result.replace(regEx, replacement); | ||
}, string); | ||
}; | ||
var unescape = function unescape(string) { | ||
return unescapePairs.reduce(function (result, _ref2) { | ||
var regEx = _ref2[0], | ||
replacement = _ref2[1]; | ||
return result.replace(regEx, replacement); | ||
}, string); | ||
}; | ||
var delimiters = ['|', '^', '&']; | ||
var parseSegment = function parseSegment(segmentRaw) { | ||
var segment = segmentRaw.trim(); | ||
var result = {}; | ||
var parseForDelimiter = function parseForDelimiter(subject, address, delimiter) { | ||
var parts = subject.split(delimiter); | ||
var nextDelimiter = delimiters[delimiters.indexOf(delimiter) + 1]; | ||
var isHeader = address === 'MSH'; | ||
parts.forEach(function (part, index) { | ||
var indexAddend = isHeader && delimiter === '|' ? 2 : 1; | ||
var currentAddress = address + '.' + (index + indexAddend); | ||
var isDelimiterField = currentAddress === 'MSH.2'; | ||
if (isDelimiterField) { | ||
result[currentAddress] = part; | ||
} else if (nextDelimiter && part.includes(nextDelimiter)) { | ||
parseForDelimiter(part, currentAddress, nextDelimiter); | ||
} else { | ||
if (part) { | ||
// const toAdd = part.includes('~') ? part.split('~') : part | ||
result[currentAddress] = unescape(part); | ||
} else { | ||
result[currentAddress] = ''; | ||
} | ||
} | ||
}); | ||
}; | ||
var split = segment.split('|'); | ||
parseForDelimiter(split.slice(1).join('|'), split[0], '|'); | ||
return result; | ||
}; | ||
var parse = function parse(message) { | ||
return message.trim().split(endOfLineRe).map(function (seg) { | ||
return parseSegment(seg); | ||
}); | ||
}; | ||
var digitRe = /\d+/g; | ||
var decrementEachValue = function decrementEachValue(str) { | ||
return str.replace(digitRe, function (item) { | ||
return Number(item) - 1; | ||
}); | ||
}; | ||
var serializeSegment = function serializeSegment(obj) { | ||
var keys = Object.keys(obj); | ||
var segmentId = keys[0].split('.')[0]; | ||
var isMessageHeader = segmentId === 'MSH'; | ||
var result = []; | ||
keys.forEach(function (key) { | ||
var value = obj[key]; | ||
var splitKey = key.split('.'); | ||
dset(result, decrementEachValue(splitKey.slice(1).join('.')), value); | ||
return result; | ||
}); | ||
var join = function join(item, depth) { | ||
if (depth === void 0) { | ||
depth = 0; | ||
} | ||
var result = ''; | ||
var entryHasChildren = false; | ||
for (var i = 0, l = item.length; i < l; i++) { | ||
var entry = item[i]; | ||
var activeDelimiter = delimiters[depth]; | ||
var toAdd = entry || ''; | ||
if (typeof toAdd === 'object') { | ||
entryHasChildren = true; | ||
toAdd = join(toAdd, depth + 1); | ||
} else { | ||
var isEncodingCharsSegment = isMessageHeader && depth === 0 && (i === 0 || i === 1); | ||
if (isEncodingCharsSegment) { | ||
toAdd = ''; | ||
activeDelimiter = ''; | ||
} else { | ||
// console.log('ENTRY HAS CHILDRE', entryHasChildren) | ||
if (!entryHasChildren) { | ||
toAdd = escape(toAdd); | ||
} | ||
} | ||
} | ||
result += (i === 0 ? '' : activeDelimiter) + toAdd; | ||
} | ||
return result; | ||
}; | ||
var joined = join(result).replace(escapeCharacterPair[0], function (match) { | ||
return match.slice(0, 1) + escapeCharacterPair[1] + match.slice(-1); | ||
}); | ||
return segmentId + (isMessageHeader ? '|^~\\&' : '|') + joined; | ||
}; | ||
var serialize = function serialize(arrayOfSegments) { | ||
return arrayOfSegments.map(serializeSegment).join('\r'); | ||
}; | ||
var messageHead = 'MSH|^'; | ||
var getMessages = function getMessages(messageString) { | ||
var messages = []; | ||
var idx = messageString.indexOf(messageHead); | ||
while (idx !== -1) { | ||
var startingIndex = idx || 0; | ||
idx = messageString.indexOf(messageHead, idx + 1); | ||
if (idx !== -1) { | ||
messages.push(messageString.slice(startingIndex, idx).trim()); | ||
} else { | ||
messages.push(messageString.slice(startingIndex).trim()); | ||
} | ||
} | ||
return messages; | ||
}; | ||
export { endOfLineRe, getMessages, parse, serialize }; | ||
//# sourceMappingURL=hl7.module.js.map |
@@ -1,2 +0,199 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("dset")):"function"==typeof define&&define.amd?define(["exports","dset"],n):n((e||self).tinyHl7={},e.dset)}(this,function(e,n){var r=/\r\n|\r|\n/g,t=[[/\\F\\/g,"|"],[/\\S\\/g,"^"],[/\\T\\/g,"&"],[/\\E\\/g,"\\"]],i=[/(?:[^EFSTR])\\(?:[^EFSTR])/g,"\\E\\"],o=[[/\|/g,"\\F\\"],[/\^/g,"\\S\\"],[/\&/g,"\\T\\"]],u=["|","^","&"],f=/\d+/g,c=function(e){var r=Object.keys(e),t=r[0].split(".")[0],c="MSH"===t,s=[];r.forEach(function(r){var t=e[r],i=r.split(".");return n.dset(s,i.slice(1).join(".").replace(f,function(e){return Number(e)-1}),t),s});var a=function e(n,r){void 0===r&&(r=0);for(var t="",i=!1,f=0,s=n.length;f<s;f++){var a=u[r],l=n[f]||"";"object"==typeof l?(i=!0,l=e(l,r+1)):!c||0!==r||0!==f&&1!==f?i||(l=o.reduce(function(e,n){return e.replace(n[0],n[1])},l)):(l="",a=""),t+=(0===f?"":a)+l}return t}(s).replace(i[0],function(e){return e.slice(0,1)+i[1]+e.slice(-1)});return t+(c?"|^~\\&":"|")+a},s="MSH|^";e.endOfLineRe=r,e.getMessages=function(e){for(var n=[],r=e.indexOf(s);-1!==r;){var t=r||0;r=e.indexOf(s,r+1),n.push(-1!==r?e.slice(t,r).trim():e.slice(t).trim())}return n},e.parse=function(e){return e.trim().split(r).map(function(e){return n=e.trim(),r={},function e(n,i,o){var f=n.split(o),c=u[u.indexOf(o)+1],s="MSH"===i;f.forEach(function(n,u){var f=i+"."+(u+(s&&"|"===o?2:1));"MSH.2"===f?r[f]=n:c&&n.includes(c)?e(n,f,c):r[f]=n?t.reduce(function(e,n){return e.replace(n[0],n[1])},n):""})}((i=n.split("|")).slice(1).join("|"),i[0],"|"),r;var n,r,i})},e.serialize=function(e){return e.map(c).join("\r")}}); | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, factory(global.tinyHl7 = {})); | ||
})(this, (function (exports) { | ||
/* | ||
dset inlined here, including license as per license in dset. | ||
The MIT License (MIT) | ||
Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (lukeed.com) | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
var dset = function dset(obj, keys, val) { | ||
keys.split && (keys = keys.split('.')); | ||
var i = 0, | ||
l = keys.length, | ||
t = obj, | ||
x, | ||
k; | ||
for (; i < l;) { | ||
k = keys[i++]; | ||
if (k === '__proto__' || k === 'constructor' || k === 'prototype') continue; | ||
t = t[k] = i === l ? val : (x = t[k]) != null ? x : keys[i] * 0 !== 0 || !!~keys[i].indexOf('.') ? {} : []; | ||
} | ||
}; | ||
var endOfLineRe = /\r\n|\r|\n/g; | ||
var unescapePairs = [[/\\F\\/g, '|'], [/\\S\\/g, '^'], [/\\T\\/g, '&'], [/\\E\\/g, '\\'] // [/\\R\\/g, '~'] | ||
]; | ||
var escapeCharacterPair = [/(?:[^EFSTR])\\(?:[^EFSTR])/g, '\\E\\']; | ||
var escapePairs = [[/\|/g, '\\F\\'], [/\^/g, '\\S\\'], [/\&/g, '\\T\\'] // [/\~/g, '\\R\\'] | ||
]; | ||
var escape = function escape(string) { | ||
return escapePairs.reduce(function (result, _ref) { | ||
var regEx = _ref[0], | ||
replacement = _ref[1]; | ||
return result.replace(regEx, replacement); | ||
}, string); | ||
}; | ||
var unescape = function unescape(string) { | ||
return unescapePairs.reduce(function (result, _ref2) { | ||
var regEx = _ref2[0], | ||
replacement = _ref2[1]; | ||
return result.replace(regEx, replacement); | ||
}, string); | ||
}; | ||
var delimiters = ['|', '^', '&']; | ||
var parseSegment = function parseSegment(segmentRaw) { | ||
var segment = segmentRaw.trim(); | ||
var result = {}; | ||
var parseForDelimiter = function parseForDelimiter(subject, address, delimiter) { | ||
var parts = subject.split(delimiter); | ||
var nextDelimiter = delimiters[delimiters.indexOf(delimiter) + 1]; | ||
var isHeader = address === 'MSH'; | ||
parts.forEach(function (part, index) { | ||
var indexAddend = isHeader && delimiter === '|' ? 2 : 1; | ||
var currentAddress = address + '.' + (index + indexAddend); | ||
var isDelimiterField = currentAddress === 'MSH.2'; | ||
if (isDelimiterField) { | ||
result[currentAddress] = part; | ||
} else if (nextDelimiter && part.includes(nextDelimiter)) { | ||
parseForDelimiter(part, currentAddress, nextDelimiter); | ||
} else { | ||
if (part) { | ||
// const toAdd = part.includes('~') ? part.split('~') : part | ||
result[currentAddress] = unescape(part); | ||
} else { | ||
result[currentAddress] = ''; | ||
} | ||
} | ||
}); | ||
}; | ||
var split = segment.split('|'); | ||
parseForDelimiter(split.slice(1).join('|'), split[0], '|'); | ||
return result; | ||
}; | ||
var parse = function parse(message) { | ||
return message.trim().split(endOfLineRe).map(function (seg) { | ||
return parseSegment(seg); | ||
}); | ||
}; | ||
var digitRe = /\d+/g; | ||
var decrementEachValue = function decrementEachValue(str) { | ||
return str.replace(digitRe, function (item) { | ||
return Number(item) - 1; | ||
}); | ||
}; | ||
var serializeSegment = function serializeSegment(obj) { | ||
var keys = Object.keys(obj); | ||
var segmentId = keys[0].split('.')[0]; | ||
var isMessageHeader = segmentId === 'MSH'; | ||
var result = []; | ||
keys.forEach(function (key) { | ||
var value = obj[key]; | ||
var splitKey = key.split('.'); | ||
dset(result, decrementEachValue(splitKey.slice(1).join('.')), value); | ||
return result; | ||
}); | ||
var join = function join(item, depth) { | ||
if (depth === void 0) { | ||
depth = 0; | ||
} | ||
var result = ''; | ||
var entryHasChildren = false; | ||
for (var i = 0, l = item.length; i < l; i++) { | ||
var entry = item[i]; | ||
var activeDelimiter = delimiters[depth]; | ||
var toAdd = entry || ''; | ||
if (typeof toAdd === 'object') { | ||
entryHasChildren = true; | ||
toAdd = join(toAdd, depth + 1); | ||
} else { | ||
var isEncodingCharsSegment = isMessageHeader && depth === 0 && (i === 0 || i === 1); | ||
if (isEncodingCharsSegment) { | ||
toAdd = ''; | ||
activeDelimiter = ''; | ||
} else { | ||
// console.log('ENTRY HAS CHILDRE', entryHasChildren) | ||
if (!entryHasChildren) { | ||
toAdd = escape(toAdd); | ||
} | ||
} | ||
} | ||
result += (i === 0 ? '' : activeDelimiter) + toAdd; | ||
} | ||
return result; | ||
}; | ||
var joined = join(result).replace(escapeCharacterPair[0], function (match) { | ||
return match.slice(0, 1) + escapeCharacterPair[1] + match.slice(-1); | ||
}); | ||
return segmentId + (isMessageHeader ? '|^~\\&' : '|') + joined; | ||
}; | ||
var serialize = function serialize(arrayOfSegments) { | ||
return arrayOfSegments.map(serializeSegment).join('\r'); | ||
}; | ||
var messageHead = 'MSH|^'; | ||
var getMessages = function getMessages(messageString) { | ||
var messages = []; | ||
var idx = messageString.indexOf(messageHead); | ||
while (idx !== -1) { | ||
var startingIndex = idx || 0; | ||
idx = messageString.indexOf(messageHead, idx + 1); | ||
if (idx !== -1) { | ||
messages.push(messageString.slice(startingIndex, idx).trim()); | ||
} else { | ||
messages.push(messageString.slice(startingIndex).trim()); | ||
} | ||
} | ||
return messages; | ||
}; | ||
exports.endOfLineRe = endOfLineRe; | ||
exports.getMessages = getMessages; | ||
exports.parse = parse; | ||
exports.serialize = serialize; | ||
})); | ||
//# sourceMappingURL=hl7.umd.js.map |
{ | ||
"name": "tiny-hl7", | ||
"version": "1.0.5", | ||
"version": "1.0.7", | ||
"description": "", | ||
@@ -12,3 +12,3 @@ "source": "src/hl7.js", | ||
"test": "tape -r esm test", | ||
"build": "rm -rf dist && microbundle", | ||
"build": "rm -rf dist && microbundle --external=dset --no-compress", | ||
"dev": "microbundle watch" | ||
@@ -25,3 +25,3 @@ }, | ||
"glob": "^7.1.6", | ||
"microbundle": "0.15.0", | ||
"microbundle": "0.15.1", | ||
"tape": "^5.0.1" | ||
@@ -33,6 +33,3 @@ }, | ||
"singleQuote": true | ||
}, | ||
"dependencies": { | ||
"dset": "3.1.2" | ||
} | ||
} |
@@ -116,2 +116,3 @@ # tiny-hl7 | ||
- `1.0.7` - inlining `dset` lib (including its license). | ||
- `1.0.3` - fixing regexp usage broken in safari | ||
@@ -118,0 +119,0 @@ - `1.0.2` - fixing dset import that was causing a bug in serialize |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
81254
121.61%0
-100%621
2117.86%1
-66.67%124
0.81%1
Infinity%- Removed
- Removed