UNPKG

@wearemothership/dicom-character-set

Version:

Converts bytes of encoded DICOM text to Javascript DOMString

3 lines 7.03 kB
/*! @wearemothership/dicom-character-set - 1.0.4-opt.1 - 2021-03-22 | (c) 2018 Radialogica, LLC | https://github.com/radialogica/dicom-character-set */ !function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports["dicom-character-set"]=n():e["dicom-character-set"]=n()}(this,function(){return(()=>{"use strict";var o={d:(e,n)=>{for(var t in n)o.o(n,t)&&!o.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:n[t]})},o:(e,n)=>Object.prototype.hasOwnProperty.call(e,n),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},e={};o.r(e),o.d(e,{characterSets:()=>i,convertBytes:()=>function(e,n,t){return g(y,r,e,n,t)},convertBytesPromise:()=>function(e,n,t){return g(P,m,e,n,t)}});var n={codeElement:"G0",escapeSequence:[27,40,66],encoding:"windows-1252",isASCII:!0,bytesPerCodePoint:1},i={"ISO_IR 6":{encoding:"utf-8"},"ISO_IR 100":{encoding:"windows-1252"},"ISO_IR 101":{encoding:"iso-8859-2"},"ISO_IR 109":{encoding:"iso-8859-3"},"ISO_IR 110":{encoding:"iso-8859-4"},"ISO_IR 144":{encoding:"iso-8859-5"},"ISO_IR 127":{encoding:"iso-8859-6"},"ISO_IR 126":{encoding:"iso-8859-7"},"ISO_IR 138":{encoding:"iso-8859-8"},"ISO_IR 148":{encoding:"windows-1254"},"ISO_IR 13":{encoding:"shift-jis"},"ISO_IR 166":{encoding:"tis-620"},"ISO 2022 IR 6":{extension:!0,elements:[n]},"ISO 2022 IR 100":{extension:!0,elements:[n,{codeElement:"G1",escapeSequence:[27,45,65],encoding:"windows-1252",bytesPerCodePoint:1}]},"ISO 2022 IR 101":{extension:!0,elements:[n,{codeElement:"G1",escapeSequence:[27,45,66],encoding:"iso-8859-2",bytesPerCodePoint:1}]},"ISO 2022 IR 109":{extension:!0,elements:[n,{codeElement:"G1",escapeSequence:[27,45,67],encoding:"iso-8859-3",bytesPerCodePoint:1}]},"ISO 2022 IR 110":{extension:!0,elements:[n,{codeElement:"G1",escapeSequence:[27,45,68],encoding:"iso-8859-4",bytesPerCodePoint:1}]},"ISO 2022 IR 144":{extension:!0,elements:[n,{codeElement:"G1",escapeSequence:[27,45,76],encoding:"iso-8859-5",bytesPerCodePoint:1}]},"ISO 2022 IR 127":{extension:!0,elements:[n,{codeElement:"G1",escapeSequence:[27,45,71],encoding:"iso-8859-6",bytesPerCodePoint:1}]},"ISO 2022 IR 126":{extension:!0,elements:[n,{codeElement:"G1",escapeSequence:[27,45,70],encoding:"iso-8859-7",bytesPerCodePoint:1}]},"ISO 2022 IR 138":{extension:!0,elements:[n,{codeElement:"G1",escapeSequence:[27,45,72],encoding:"iso-8859-8",bytesPerCodePoint:1}]},"ISO 2022 IR 148":{extension:!0,elements:[n,{codeElement:"G1",escapeSequence:[27,45,77],encoding:"windows-1254",bytesPerCodePoint:1}]},"ISO 2022 IR 13":{extension:!0,elements:[{codeElement:"G0",escapeSequence:[27,40,74],encoding:"shift-jis",bytesPerCodePoint:1},{codeElement:"G1",escapeSequence:[27,41,73],encoding:"shift-jis",bytesPerCodePoint:1}]},"ISO 2022 IR 166":{extension:!0,elements:[n,{codeElement:"G1",escapeSequence:[27,45,84],encoding:"tis-620",bytesPerCodePoint:1}]},"ISO 2022 IR 87":{extension:!0,multiByte:!0,elements:[{codeElement:"G0",escapeSequence:[27,36,66],encoding:"euc-jp",setHighBit:!0,bytesPerCodePoint:2}]},"ISO 2022 IR 159":{extension:!0,multiByte:!0,elements:[{codeElement:"G0",escapeSequence:[27,36,40,68],encoding:"euc-jp",isJISX0212:!0,bytesPerCodePoint:2}]},"ISO 2022 IR 149":{extension:!0,multiByte:!0,elements:[{codeElement:"G1",escapeSequence:[27,36,41,67],encoding:"euc-kr",bytesPerCodePoint:2}]},"ISO 2022 IR 58":{extension:!0,multiByte:!0,elements:[{codeElement:"G1",escapeSequence:[27,36,41,65],encoding:"gb18030",bytesPerCodePoint:2}]},"ISO_IR 192":{encoding:"utf-8",multiByte:!0},GB18030:{encoding:"gb18030",multiByte:!0},GBK:{encoding:"gbk",multiByte:!0}},l=27,c=10,s=12,u=13,d=9,a=92,f=61,I=94;function S(e){return e.replace(/~/g,"‾").replace(/\\/g,"¥")}function r(e,n,t,o,r){r=b(n,t,o,r);return e+y(n.encoding,r)}function m(e,t,n,o,r){var i=b(t,n,o,r);return(""===e?Promise.resolve(""):e).then(function(n){return P(t.encoding,i).then(function(e){return n+e})})}function g(e,n,t,o,r){!function(e,n){if(n&&!(n instanceof Uint8Array))throw new Error("bytes must be a Uint8Array");if(e&&"string"!=typeof e)throw new Error("specificCharacterSet must be a string")}(t,o);t=function(e){e=e?e.split("\\").map(function(e){return e.trim().toUpperCase()}):[""];""===e[0]&&(e[0]=1<e.length?"ISO 2022 IR 6":"ISO_IR 6");if(e.some(function(e){return void 0===i[e]}))throw new Error("Invalid specific character set specified.");return function(e){for(var n=1<e.length,t=[],o=0;o<e.length;o++){var r=e[o];t.includes(r)||t.push(n?r.replace("ISO_IR","ISO 2022 IR"):r)}return t}(e=function(e){var n=i[e[0]];return!n.multiByte||n.extension?e.filter(function(e){return!i[e].multiByte||i[e].extension}):[e[0]]}(e))}(t);if(1===t.length&&!t[0].startsWith("ISO 2022"))return e(i[t[0]].encoding,o);r=r||{};return function(e,n,t,o){var r="";if(!n||0===n.length)return r;var i={G0:e[0].elements.find(function(e){return"G0"===e.codeElement}),G1:e[0].elements.find(function(e){return"G1"===e.codeElement})},c=Object.assign({},i),s=0,u=void 0,d=0;for(;d<n.length;){u=u||h(n[s],c);var a=function(e,n,t,o,r,i){for(var c=n;c<e.length;c+=t.bytesPerCodePoint){if(e[c]===l)return{escapeSequence:!0,index:c};1===t.bytesPerCodePoint&&i.includes(e[c])&&Object.assign(o,r);var s=h(e[c],o);if(t&&s!==t)return{characterSet:s,index:c}}return{index:e.length}}(n,s,u,c,i,t);d=a.index,s<d&&(r=o(r,u,n,s,d)),s=d,u=a.characterSet,a.escapeSequence&&(a=function(e,n,t){for(var o=0;o<t.length;o++)for(var r=t[o],i=0;i<r.elements.length;i++){var c=r.elements[i];if(function(e,n,t){for(var o=0;o<e.length;o++){if(t+o>=n.length)return!1;if(n[t+o]!==e[o])return!1}return!0}(c.escapeSequence,e,n))return c}throw new Error("Unknown escape sequence encountered at byte "+n)}(n,d,e),c[a.codeElement]=a,s+=a.escapeSequence.length)}return r}(t.map(function(e){return i[e]}),o,function(e){var n=(e||"").trim().toUpperCase(),e=[c,s,u,d];["UT","ST","LT"].includes(n)||e.push(a);"PN"===n&&(e.push(f),e.push(I));return e}(r.vr),n)}var p={};function y(e,n){var t=p[e];return t||(t=new TextDecoder(e),p[e]=t="shift-jis"===e?{textDecoder:t,decode:function(e){return S(t.decode(e))}}:t),t.decode(n)}function P(o,r){return new Promise(function(e){var n=new FileReader;n.onload="shift-jis"===o?function(){return e(S(n.result))}:function(){return e(n.result)};var t=new Blob([r]);n.readAsText(t,o)})}function h(e,n){return 127<e&&n.G1?n.G1:n.G0||(n.G1&&1===n.G1.bytesPerCodePoint?n.G1:i["ISO 2022 IR 6"].elements[0])}function b(e,n,t,o){var r=void 0;return e.isJISX0212?r=function(e,n,t){var o=t-n;if(o%2!=0)throw new Error("JIS X string with a character not having exactly two bytes!");for(var r=new Uint8Array(o+o/2),i=0,c=n;c<t;c+=2)r[i++]=143,r[i++]=128|e[c],r[i++]=128|e[c+1];return r}(n,t,o):((r=new Uint8Array(o-t)).set(new Uint8Array(n.buffer,n.byteOffset+t,o-t)),e.setHighBit&&function(e){for(var n=0;n<e.length;n++)e[n]|=128}(r)),r}return e})()}); //# sourceMappingURL=dicom-character-set.min.js.map