From 7a1a8ad01e37523b05239b33a7e45621acbf3f8e Mon Sep 17 00:00:00 2001 From: sasaplus1 Date: Mon, 21 Aug 2023 19:13:36 +0900 Subject: [PATCH] update dist --- dist/ltsv.cjs | 2 +- dist/ltsv.cjs.map | 2 +- dist/ltsv.js | 2 + dist/ltsv.js.map | 1 + dist/ltsv.modern.mjs | 2 +- dist/ltsv.modern.mjs.map | 2 +- dist/ltsv.module.mjs | 2 +- dist/ltsv.module.mjs.map | 2 +- dist/ltsv.umd.js | 2 +- dist/ltsv.umd.js.map | 2 +- dist/nodejs_stream/ltsv.cjs | 151 +++++++++-------- dist/nodejs_stream/ltsv.cjs.map | 2 +- dist/nodejs_stream/ltsv.js | 220 +++++++++++++++++++++++++ dist/nodejs_stream/ltsv.js.map | 1 + dist/nodejs_stream/ltsv.modern.mjs | 151 +++++++++-------- dist/nodejs_stream/ltsv.modern.mjs.map | 2 +- dist/nodejs_stream/ltsv.module.mjs | 151 +++++++++-------- dist/nodejs_stream/ltsv.module.mjs.map | 2 +- dist/src/formatter.d.ts | 52 +++--- dist/src/index.d.ts | 8 +- dist/src/nodejs_stream.d.ts | 141 ++++++++-------- dist/src/parser.d.ts | 50 +++--- dist/src/src/nodejs_stream.d.ts | 5 +- dist/src/test/formatter.d.ts | 1 + dist/src/test/nodejs_stream.d.ts | 1 + dist/src/test/parser.d.ts | 1 + dist/src/test/validator.d.ts | 1 + dist/src/test/whatwg_stream.d.ts | 1 + dist/src/types.d.ts | 6 +- dist/src/validator.d.ts | 24 +-- dist/src/whatwg_stream.d.ts | 24 +-- dist/whatwg_stream/ltsv.cjs | 2 +- dist/whatwg_stream/ltsv.cjs.map | 2 +- dist/whatwg_stream/ltsv.js | 2 + dist/whatwg_stream/ltsv.js.map | 1 + dist/whatwg_stream/ltsv.modern.mjs | 2 +- dist/whatwg_stream/ltsv.modern.mjs.map | 2 +- dist/whatwg_stream/ltsv.module.mjs | 2 +- dist/whatwg_stream/ltsv.module.mjs.map | 2 +- dist/whatwg_stream/ltsv.umd.js | 2 +- dist/whatwg_stream/ltsv.umd.js.map | 2 +- test/formatter.ts | 2 +- test/nodejs_stream.ts | 21 ++- test/parser.ts | 2 +- test/validator.ts | 18 +- test/whatwg_stream.ts | 2 +- 46 files changed, 673 insertions(+), 405 deletions(-) create mode 100644 dist/ltsv.js create mode 100644 dist/ltsv.js.map create mode 100644 dist/nodejs_stream/ltsv.js create mode 100644 dist/nodejs_stream/ltsv.js.map create mode 100644 dist/src/test/formatter.d.ts create mode 100644 dist/src/test/nodejs_stream.d.ts create mode 100644 dist/src/test/parser.d.ts create mode 100644 dist/src/test/validator.d.ts create mode 100644 dist/src/test/whatwg_stream.d.ts create mode 100644 dist/whatwg_stream/ltsv.js create mode 100644 dist/whatwg_stream/ltsv.js.map diff --git a/dist/ltsv.cjs b/dist/ltsv.cjs index a98389fb..cec73ce5 100644 --- a/dist/ltsv.cjs +++ b/dist/ltsv.cjs @@ -1,2 +1,2 @@ -function r(r){return/^[0-9A-Za-z_.-]+$/.test(r)}function t(r){return/^[\x01-\x08\x0B\x0C\x0E-\xFF]*$/.test(r)}function e(e,n){if(null===e||"object"!=typeof e)throw new TypeError("record must be an Object");for(var o=Object.keys(e),a=[],i=0,u=o.length;i -/// -/// -/// -import { Transform, TransformCallback } from 'stream'; -import { StringDecoder } from 'string_decoder'; -import { parseLine, parseLineStrict } from './parser'; -export type LtsvToJsonStreamOptions = { - encoding?: string; - objectMode?: boolean; - strict?: boolean; -}; -/** - * LTSV to JSON transform stream - */ -export declare class LtsvToJsonStream extends Transform { - /** - * chunk buffer - */ - buffer: string; - /** - * for decode chunks - */ - decoder: StringDecoder; - /** - * if true, pass object to next stream - */ - objectMode: boolean; - /** - * parser function - */ - parse: typeof parseLine | typeof parseLineStrict; - /** - * constructor - * - * @param options - */ - constructor(options?: LtsvToJsonStreamOptions); - /** - * transform and push to stream. - * - * @private - * @param text - * @param isFlush - * @param callback - */ - _push(text: string, isFlush: boolean, callback: TransformCallback): void; - /** - * _transform implementation. - * - * @private - * @param chunk - * @param encoding - * @param callback - */ - _transform(chunk: Buffer, encoding: string, callback: TransformCallback): void; - /** - * _flush implementation. - * - * @private - * @param callback - */ - _flush(callback: TransformCallback): void; -} -/** - * create LtsvToJsonStream instance. - * - * @param options - * @see LtsvToJsonStream - */ -export declare function createLtsvToJsonStream(options?: LtsvToJsonStreamOptions): LtsvToJsonStream; +/// +/// +/// +import { Transform, TransformCallback } from 'stream'; +import { StringDecoder } from 'string_decoder'; +import { parseLine, parseLineStrict } from './parser'; +export type LtsvToJsonStreamOptions = { + encoding?: BufferEncoding; + objectMode?: boolean; + strict?: boolean; +}; +/** + * LTSV to JSON transform stream + */ +export declare class LtsvToJsonStream extends Transform { + /** + * chunk buffer + */ + buffer: string; + /** + * for decode chunks + */ + decoder: StringDecoder; + /** + * if true, pass object to next stream + */ + objectMode: boolean; + /** + * parser function + */ + parse: typeof parseLine | typeof parseLineStrict; + /** + * constructor + * + * @param options + */ + constructor(options?: LtsvToJsonStreamOptions); + /** + * transform and push to stream. + * + * @private + * @param text + * @param isFlush + * @param callback + */ + _push(text: string, isFlush: boolean, callback: TransformCallback): void; + /** + * _transform implementation. + * + * @private + * @param chunk + * @param encoding + * @param callback + */ + _transform(chunk: any, _encoding: BufferEncoding, callback: TransformCallback): void; + /** + * _flush implementation. + * + * @private + * @param callback + */ + _flush(callback: TransformCallback): void; +} +/** + * create LtsvToJsonStream instance. + * + * @param options + * @see LtsvToJsonStream + */ +export declare function createLtsvToJsonStream(options?: LtsvToJsonStreamOptions): LtsvToJsonStream; diff --git a/dist/src/parser.d.ts b/dist/src/parser.d.ts index 9c2bf192..4d513e43 100644 --- a/dist/src/parser.d.ts +++ b/dist/src/parser.d.ts @@ -1,25 +1,25 @@ -import { LtsvRecord } from './types'; -/** - * parse LTSV text - * - * @param text - */ -export declare function parse(text: string): LtsvRecord[]; -/** - * parse LTSV record - * - * @param line - */ -export declare function parseLine(line: string): LtsvRecord; -/** - * parse LTSV text - * - * @param text - */ -export declare function parseStrict(text: string): LtsvRecord[]; -/** - * parse LTSV record - * - * @param line - */ -export declare function parseLineStrict(line: string): LtsvRecord; +import { LtsvRecord } from './types'; +/** + * parse LTSV text + * + * @param text + */ +export declare function parse(text: string): LtsvRecord[]; +/** + * parse LTSV record + * + * @param line + */ +export declare function parseLine(line: string): LtsvRecord; +/** + * parse LTSV text + * + * @param text + */ +export declare function parseStrict(text: string): LtsvRecord[]; +/** + * parse LTSV record + * + * @param line + */ +export declare function parseLineStrict(line: string): LtsvRecord; diff --git a/dist/src/src/nodejs_stream.d.ts b/dist/src/src/nodejs_stream.d.ts index c04a378a..e485d990 100644 --- a/dist/src/src/nodejs_stream.d.ts +++ b/dist/src/src/nodejs_stream.d.ts @@ -1,12 +1,11 @@ /// /// /// -/// import { Transform, TransformCallback } from 'stream'; import { StringDecoder } from 'string_decoder'; import { parseLine, parseLineStrict } from './parser'; export type LtsvToJsonStreamOptions = { - encoding?: string; + encoding?: BufferEncoding; objectMode?: boolean; strict?: boolean; }; @@ -53,7 +52,7 @@ export declare class LtsvToJsonStream extends Transform { * @param encoding * @param callback */ - _transform(chunk: Buffer, encoding: string, callback: TransformCallback): void; + _transform(chunk: any, _encoding: BufferEncoding, callback: TransformCallback): void; /** * _flush implementation. * diff --git a/dist/src/test/formatter.d.ts b/dist/src/test/formatter.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/src/test/formatter.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/src/test/nodejs_stream.d.ts b/dist/src/test/nodejs_stream.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/src/test/nodejs_stream.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/src/test/parser.d.ts b/dist/src/test/parser.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/src/test/parser.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/src/test/validator.d.ts b/dist/src/test/validator.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/src/test/validator.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/src/test/whatwg_stream.d.ts b/dist/src/test/whatwg_stream.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/src/test/whatwg_stream.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/src/types.d.ts b/dist/src/types.d.ts index b1e716be..564ffb42 100644 --- a/dist/src/types.d.ts +++ b/dist/src/types.d.ts @@ -1,3 +1,3 @@ -export type LtsvRecord = { - [key: string]: string; -}; +export type LtsvRecord = { + [key: string]: string; +}; diff --git a/dist/src/validator.d.ts b/dist/src/validator.d.ts index 33aedb08..f5743331 100644 --- a/dist/src/validator.d.ts +++ b/dist/src/validator.d.ts @@ -1,12 +1,12 @@ -/** - * validate label - * - * @param label - */ -export declare function isValidLabel(label: string): boolean; -/** - * validate for value - * - * @param value - */ -export declare function isValidValue(value: string): boolean; +/** + * validate label + * + * @param label + */ +export declare function isValidLabel(label: string): boolean; +/** + * validate for value + * + * @param value + */ +export declare function isValidValue(value: string): boolean; diff --git a/dist/src/whatwg_stream.d.ts b/dist/src/whatwg_stream.d.ts index cab9a318..8f1ef2b6 100644 --- a/dist/src/whatwg_stream.d.ts +++ b/dist/src/whatwg_stream.d.ts @@ -1,12 +1,12 @@ -import { LtsvRecord } from './types'; -export type LtsvToJsonStreamOptions = { - objectMode?: boolean; - strict?: boolean; -}; -/** - * LTSV to JSON transform stream - * - * @param options - */ -export declare function LtsvToJsonStream(options?: LtsvToJsonStreamOptions): Transformer; -export declare function createLtsvToJsonStream(options?: LtsvToJsonStreamOptions): TransformStream; +import { LtsvRecord } from './types'; +export type LtsvToJsonStreamOptions = { + objectMode?: boolean; + strict?: boolean; +}; +/** + * LTSV to JSON transform stream + * + * @param options + */ +export declare function LtsvToJsonStream(options?: LtsvToJsonStreamOptions): Transformer; +export declare function createLtsvToJsonStream(options?: LtsvToJsonStreamOptions): TransformStream; diff --git a/dist/whatwg_stream/ltsv.cjs b/dist/whatwg_stream/ltsv.cjs index b7fcd56d..de63875f 100644 --- a/dist/whatwg_stream/ltsv.cjs +++ b/dist/whatwg_stream/ltsv.cjs @@ -1,2 +1,2 @@ -function r(r,e){var t=String(r),n=t.indexOf(":");if(-1===n)throw new SyntaxError('field separator is not found: "'+t+'"');var o=t.slice(0,n),i=t.slice(n+1);if(e&&!function(r){return/^[0-9A-Za-z_.-]+$/.test(r)}(o))throw new SyntaxError('unexpected character in label: "'+o+'"');if(e&&!function(r){return/^[\x01-\x08\x0B\x0C\x0E-\xFF]*$/.test(r)}(i))throw new SyntaxError('unexpected character in value: "'+i+'"');return{label:o,value:i}}function e(e,t){for(var n=String(e).replace(/(?:\r?\n)+$/,"").split("\t"),o={},i=0,a=n.length;i\n): void {\n let next = 0;\n let last = 0;\n let error: Error | null = null;\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n let index = text.indexOf('\\n', next);\n\n if (index === -1) {\n if (isFlush && next < text.length) {\n // NOTE: subtract 1 from text.length,\n // NOTE: because add 1 to index when slice.\n index = text.length - 1;\n } else {\n break;\n }\n }\n\n // NOTE: include `\\n`.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: -----------------|\n const line = text.slice(next, index + 1);\n\n let record: LtsvRecord = {};\n\n try {\n record = this.parse(line);\n } catch (e) {\n if (e instanceof Error) {\n error = e;\n }\n }\n\n if (error) {\n break;\n }\n\n controller.enqueue(this.objectMode ? record : JSON.stringify(record));\n\n // NOTE: save next start index.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: ------------------|\n last = next = index + 1;\n }\n\n this.buffer = text.slice(last);\n\n if (error) {\n controller.error(error);\n }\n}\n\n/**\n * LTSV to JSON transform stream\n *\n * @param options\n */\nexport function LtsvToJsonStream(\n options: LtsvToJsonStreamOptions = {\n objectMode: false,\n strict: false\n }\n): Transformer {\n const { objectMode = false, strict = false } = options;\n\n const instance: LtsvToJsonStreamInstance = {\n buffer: '',\n objectMode,\n parse: strict ? parseLineStrict : parseLine\n };\n\n return {\n /**\n * transform implementation.\n *\n * @param chunk\n * @param controller\n */\n transform(\n chunk: string,\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer + chunk, false, controller);\n },\n /**\n * flush implementation.\n *\n * @param controller\n */\n flush(\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer, true, controller);\n }\n };\n}\n\nexport function createLtsvToJsonStream(\n options?: LtsvToJsonStreamOptions\n): TransformStream {\n return new TransformStream(LtsvToJsonStream(options));\n}\n"],"names":["splitField","chunk","strict","field","String","index","indexOf","SyntaxError","label","slice","value","test","isValidLabel","isValidValue","baseParseLine","line","fields","replace","split","record","i","len","length","_splitField","parseLine","parseLineStrict","push","text","isFlush","controller","next","last","error","this","parse","e","Error","enqueue","objectMode","JSON","stringify","buffer","LtsvToJsonStream","options","_options$objectMode","_options$strict","instance","transform","call","flush","TransformStream"],"mappings":"AAiBA,SAASA,EAAWC,EAAeC,GACjC,IAAMC,EAAQC,OAAOH,GACfI,EAAQF,EAAMG,QAAQ,KAE5B,IAAe,IAAXD,EACF,UAAUE,8CAA8CJ,OAG1D,IAAMK,EAAQL,EAAMM,MAAM,EAAGJ,GACvBK,EAAQP,EAAMM,MAAMJ,EAAQ,GAElC,GAAIH,aCvBuBM,GAC3B,0BAA2BG,KAAKH,EAClC,CDqBiBI,CAAaJ,GAC1B,UAAUD,+CAA+CC,OAG3D,GAAIN,aClBuBQ,GAE3B,wCAAyCC,KAAKD,EAChD,CDeiBG,CAAaH,GAC1B,UAAUH,+CAA+CG,OAG3D,MAAO,CACLF,MAAAA,EACAE,MAAAA,EAEJ,CASA,SAASI,EAAcC,EAAcb,GAOnC,IANA,IAAMc,EAASZ,OAAOW,GACnBE,QAAQ,cAAe,IACvBC,MAAM,MAEHC,EAAqB,GAElBC,EAAI,EAAGC,EAAML,EAAOM,OAAQF,EAA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9C"} \ No newline at end of file +{"version":3,"file":"ltsv.cjs","sources":["../../src/parser.ts","../../src/validator.ts","../../src/whatwg_stream.ts"],"sourcesContent":["import { isValidLabel, isValidValue } from './validator';\n\nimport { LtsvRecord } from './types';\n\ntype LtsvField = {\n label: string;\n value: string;\n};\n\n/**\n * split to label and value from field\n *\n * @private\n * @param chunk\n * @param strict\n * @throws {SyntaxError}\n * @throws {TypeError}\n */\nfunction splitField(chunk: string | undefined, strict: boolean): LtsvField {\n if (chunk === undefined) {\n throw new TypeError('chunk is undefined');\n }\n\n const index = chunk.indexOf(':');\n\n if (index === -1) {\n throw new SyntaxError(`field separator is not found: \"${chunk}\"`);\n }\n\n const label = chunk.slice(0, index);\n const value = chunk.slice(index + 1);\n\n if (strict && !isValidLabel(label)) {\n throw new SyntaxError(`unexpected character in label: \"${label}\"`);\n }\n\n if (strict && !isValidValue(value)) {\n throw new SyntaxError(`unexpected character in value: \"${value}\"`);\n }\n\n return {\n label,\n value\n };\n}\n\n/**\n * parse LTSV record\n *\n * @private\n * @param line\n * @param strict\n */\nfunction baseParseLine(line: string | undefined, strict: boolean): LtsvRecord {\n const fields = String(line)\n .replace(/(?:\\r?\\n)+$/, '')\n .split('\\t');\n\n const record: LtsvRecord = {};\n\n for (let i = 0, len = fields.length; i < len; ++i) {\n const { label, value } = splitField(fields[i], strict);\n\n record[label] = value;\n }\n\n return record;\n}\n\n/**\n * parse LTSV text\n *\n * @private\n * @param text\n * @param strict\n */\nfunction baseParse(text: string, strict: boolean): LtsvRecord[] {\n const lines = String(text)\n .replace(/(?:\\r?\\n)+$/, '')\n .split(/\\r?\\n/);\n\n const records: LtsvRecord[] = [];\n\n for (let i = 0, len = lines.length; i < len; ++i) {\n records[i] = baseParseLine(lines[i], strict);\n }\n\n return records;\n}\n\n/**\n * parse LTSV text\n *\n * @param text\n */\nexport function parse(text: string): LtsvRecord[] {\n return baseParse(text, false);\n}\n\n/**\n * parse LTSV record\n *\n * @param line\n */\nexport function parseLine(line: string): LtsvRecord {\n return baseParseLine(line, false);\n}\n\n/**\n * parse LTSV text\n *\n * @param text\n */\nexport function parseStrict(text: string): LtsvRecord[] {\n return baseParse(text, true);\n}\n\n/**\n * parse LTSV record\n *\n * @param line\n */\nexport function parseLineStrict(line: string): LtsvRecord {\n return baseParseLine(line, true);\n}\n","/**\n * validate label\n *\n * @param label\n */\nexport function isValidLabel(label: string): boolean {\n return /^[0-9A-Za-z_.-]+$/.test(label);\n}\n\n/**\n * validate for value\n *\n * @param value\n */\nexport function isValidValue(value: string): boolean {\n // eslint-disable-next-line no-control-regex\n return /^[\\x01-\\x08\\x0B\\x0C\\x0E-\\xFF]*$/.test(value);\n}\n","import { parseLine, parseLineStrict } from './parser';\nimport { LtsvRecord } from './types';\n\nexport type LtsvToJsonStreamOptions = {\n objectMode?: boolean;\n strict?: boolean;\n};\n\ntype LtsvToJsonStreamInstance = {\n buffer: string;\n objectMode: boolean;\n parse: typeof parseLine | typeof parseLineStrict;\n};\n\n/**\n * transform and push to stream\n *\n * @param text\n * @param isFlush\n * @param controller\n */\nfunction push(\n this: LtsvToJsonStreamInstance,\n text: string,\n isFlush: boolean,\n controller: TransformStreamDefaultController\n): void {\n let next = 0;\n let last = 0;\n let error: Error | null = null;\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n let index = text.indexOf('\\n', next);\n\n if (index === -1) {\n if (isFlush && next < text.length) {\n // NOTE: subtract 1 from text.length,\n // NOTE: because add 1 to index when slice.\n index = text.length - 1;\n } else {\n break;\n }\n }\n\n // NOTE: include `\\n`.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: -----------------|\n const line = text.slice(next, index + 1);\n\n let record: LtsvRecord = {};\n\n try {\n record = this.parse(line);\n } catch (e) {\n if (e instanceof Error) {\n error = e;\n }\n }\n\n if (error) {\n break;\n }\n\n controller.enqueue(this.objectMode ? record : JSON.stringify(record));\n\n // NOTE: save next start index.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: ------------------|\n last = next = index + 1;\n }\n\n this.buffer = text.slice(last);\n\n if (error) {\n controller.error(error);\n }\n}\n\n/**\n * LTSV to JSON transform stream\n *\n * @param options\n */\nexport function LtsvToJsonStream(\n options: LtsvToJsonStreamOptions = {\n objectMode: false,\n strict: false\n }\n): Transformer {\n const { objectMode = false, strict = false } = options;\n\n const instance: LtsvToJsonStreamInstance = {\n buffer: '',\n objectMode,\n parse: strict ? parseLineStrict : parseLine\n };\n\n return {\n /**\n * transform implementation.\n *\n * @param chunk\n * @param controller\n */\n transform(\n chunk: string,\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer + chunk, false, controller);\n },\n /**\n * flush implementation.\n *\n * @param controller\n */\n flush(\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer, true, controller);\n }\n };\n}\n\nexport function createLtsvToJsonStream(\n options?: LtsvToJsonStreamOptions\n): TransformStream {\n return new TransformStream(LtsvToJsonStream(options));\n}\n"],"names":["splitField","chunk","strict","undefined","TypeError","index","indexOf","SyntaxError","label","slice","value","test","isValidLabel","isValidValue","baseParseLine","line","fields","String","replace","split","record","i","len","length","_splitField","parseLine","parseLineStrict","push","text","isFlush","controller","next","last","error","this","parse","e","Error","enqueue","objectMode","JSON","stringify","buffer","LtsvToJsonStream","options","_options$objectMode","_options$strict","instance","transform","call","flush","TransformStream"],"mappings":"AAkBA,SAASA,EAAWC,EAA2BC,GAC7C,QAAcC,IAAVF,EACF,UAAUG,UAAU,sBAGtB,IAAMC,EAAQJ,EAAMK,QAAQ,KAE5B,IAAe,IAAXD,EACF,UAAUE,8CAA8CN,OAG1D,IAAMO,EAAQP,EAAMQ,MAAM,EAAGJ,GACvBK,EAAQT,EAAMQ,MAAMJ,EAAQ,GAElC,GAAIH,aC3BuBM,GAC3B,0BAA2BG,KAAKH,EAClC,CDyBiBI,CAAaJ,GAC1B,UAAUD,+CAA+CC,OAG3D,GAAIN,aCtBuBQ,GA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QA,EAKxBV,eALgCW,GAAQA,EAKxBpB,EAAkBD,GAGpC,MAAO,CAOLuB,mBACE/C,EACA6B,GAEAH,EAAKsB,KAAKF,EAAUA,EAASL,OAASzC,GAAO,EAAO6B,EACtD,EAMAoB,eACEpB,GAEAH,EAAKsB,KAAKF,EAAUA,EAASL,QAAQ,EAAMZ,EAC7C,EAEJ,oEAGEc,GAEA,WAAWO,gBAAgBR,EAAiBC,GAC9C"} \ No newline at end of file diff --git a/dist/whatwg_stream/ltsv.js b/dist/whatwg_stream/ltsv.js new file mode 100644 index 00000000..806ff24a --- /dev/null +++ b/dist/whatwg_stream/ltsv.js @@ -0,0 +1,2 @@ +function r(r,e){if(void 0===r)throw new TypeError("chunk is undefined");var t=r.indexOf(":");if(-1===t)throw new SyntaxError('field separator is not found: "'+r+'"');var n=r.slice(0,t),o=r.slice(t+1);if(e&&!function(r){return/^[0-9A-Za-z_.-]+$/.test(r)}(n))throw new SyntaxError('unexpected character in label: "'+n+'"');if(e&&!function(r){return/^[\x01-\x08\x0B\x0C\x0E-\xFF]*$/.test(r)}(o))throw new SyntaxError('unexpected character in value: "'+o+'"');return{label:n,value:o}}function e(e,t){for(var n=String(e).replace(/(?:\r?\n)+$/,"").split("\t"),o={},i=0,a=n.length;i\n): void {\n let next = 0;\n let last = 0;\n let error: Error | null = null;\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n let index = text.indexOf('\\n', next);\n\n if (index === -1) {\n if (isFlush && next < text.length) {\n // NOTE: subtract 1 from text.length,\n // NOTE: because add 1 to index when slice.\n index = text.length - 1;\n } else {\n break;\n }\n }\n\n // NOTE: include `\\n`.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: -----------------|\n const line = text.slice(next, index + 1);\n\n let record: LtsvRecord = {};\n\n try {\n record = this.parse(line);\n } catch (e) {\n if (e instanceof Error) {\n error = e;\n }\n }\n\n if (error) {\n break;\n }\n\n controller.enqueue(this.objectMode ? record : JSON.stringify(record));\n\n // NOTE: save next start index.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: ------------------|\n last = next = index + 1;\n }\n\n this.buffer = text.slice(last);\n\n if (error) {\n controller.error(error);\n }\n}\n\n/**\n * LTSV to JSON transform stream\n *\n * @param options\n */\nexport function LtsvToJsonStream(\n options: LtsvToJsonStreamOptions = {\n objectMode: false,\n strict: false\n }\n): Transformer {\n const { objectMode = false, strict = false } = options;\n\n const instance: LtsvToJsonStreamInstance = {\n buffer: '',\n objectMode,\n parse: strict ? parseLineStrict : parseLine\n };\n\n return {\n /**\n * transform implementation.\n *\n * @param chunk\n * @param controller\n */\n transform(\n chunk: string,\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer + chunk, false, controller);\n },\n /**\n * flush implementation.\n *\n * @param controller\n */\n flush(\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer, true, controller);\n }\n };\n}\n\nexport function createLtsvToJsonStream(\n options?: LtsvToJsonStreamOptions\n): TransformStream {\n return new TransformStream(LtsvToJsonStream(options));\n}\n"],"names":["splitField","chunk","strict","undefined","TypeError","index","indexOf","SyntaxError","label","slice","value","test","isValidLabel","isValidValue","baseParseLine","line","fields","String","replace","split","record","i","len","length","_splitField","parseLine","parseLineStrict","push","text","isFlush","controller","next","last","error","this","parse","e","Error","enqueue","objectMode","JSON","stringify","buffer","LtsvToJsonStream","options","_options$objectMode","_options$strict","instance","transform","call","flush","TransformStream"],"mappings":"AAkBA,SAASA,EAAWC,EAA2BC,GAC7C,QAAcC,IAAVF,EACF,UAAUG,UAAU,sBAGtB,IAAMC,EAAQJ,EAAMK,QAAQ,KAE5B,IAAe,IAAXD,EACF,UAAUE,8CAA8CN,OAG1D,IAAMO,EAAQP,EAAMQ,MAAM,EAAGJ,GACvBK,EAAQT,EAAMQ,MAAMJ,EAAQ,GAElC,GAAIH,aC3BuBM,GAC3B,0BAA2BG,KAAKH,EAClC,CDyBiBI,CAAaJ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C,OAEtB6C,EAAqC,CACzCL,OAAQ,GACRH,oBAJgBM,GAAQA,EAKxBV,eALgCW,GAAQA,EAKxBpB,EAAkBD,GAGpC,MAAO,CAOLuB,mBACE/C,EACA6B,GAEAH,EAAKsB,KAAKF,EAAUA,EAASL,OAASzC,GAAO,EAAO6B,EACtD,EAMAoB,eACEpB,GAEAH,EAAKsB,KAAKF,EAAUA,EAASL,QAAQ,EAAMZ,EAC7C,EAEJ,oEAGEc,GAEA,WAAWO,gBAAgBR,EAAiBC,GAC9C"} \ No newline at end of file diff --git a/dist/whatwg_stream/ltsv.modern.mjs b/dist/whatwg_stream/ltsv.modern.mjs index 689d108d..731ba9a4 100644 --- a/dist/whatwg_stream/ltsv.modern.mjs +++ b/dist/whatwg_stream/ltsv.modern.mjs @@ -1,2 +1,2 @@ -function e(e,t){const r=String(e),n=r.indexOf(":");if(-1===n)throw new SyntaxError(`field separator is not found: "${r}"`);const o=r.slice(0,n),c=r.slice(n+1);if(t&&!function(e){return/^[0-9A-Za-z_.-]+$/.test(e)}(o))throw new SyntaxError(`unexpected character in label: "${o}"`);if(t&&!function(e){return/^[\x01-\x08\x0B\x0C\x0E-\xFF]*$/.test(e)}(c))throw new SyntaxError(`unexpected character in value: "${c}"`);return{label:o,value:c}}function t(t,r){const n=String(t).replace(/(?:\r?\n)+$/,"").split("\t"),o={};for(let t=0,c=n.length;t\n): void {\n let next = 0;\n let last = 0;\n let error: Error | null = null;\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n let index = text.indexOf('\\n', next);\n\n if (index === -1) {\n if (isFlush && next < text.length) {\n // NOTE: subtract 1 from text.length,\n // NOTE: because add 1 to index when slice.\n index = text.length - 1;\n } else {\n break;\n }\n }\n\n // NOTE: include `\\n`.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: -----------------|\n const line = text.slice(next, index + 1);\n\n let record: LtsvRecord = {};\n\n try {\n record = this.parse(line);\n } catch (e) {\n if (e instanceof Error) {\n error = e;\n }\n }\n\n if (error) {\n break;\n }\n\n controller.enqueue(this.objectMode ? record : JSON.stringify(record));\n\n // NOTE: save next start index.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: ------------------|\n last = next = index + 1;\n }\n\n this.buffer = text.slice(last);\n\n if (error) {\n controller.error(error);\n }\n}\n\n/**\n * LTSV to JSON transform stream\n *\n * @param options\n */\nexport function LtsvToJsonStream(\n options: LtsvToJsonStreamOptions = {\n objectMode: false,\n strict: false\n }\n): Transformer {\n const { objectMode = false, strict = false } = options;\n\n const instance: LtsvToJsonStreamInstance = {\n buffer: '',\n objectMode,\n parse: strict ? parseLineStrict : parseLine\n };\n\n return {\n /**\n * transform implementation.\n *\n * @param chunk\n * @param controller\n */\n transform(\n chunk: string,\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer + chunk, false, controller);\n },\n /**\n * flush implementation.\n *\n * @param controller\n */\n flush(\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer, true, controller);\n }\n };\n}\n\nexport function createLtsvToJsonStream(\n options?: LtsvToJsonStreamOptions\n): TransformStream {\n return new TransformStream(LtsvToJsonStream(options));\n}\n"],"names":["splitField","chunk","strict","field","String","index","indexOf","SyntaxError","label","slice","value","test","isValidLabel","isValidValue","baseParseLine","line","fields","replace","split","record","i","len","length","parseLine","parseLineStrict","push","text","isFlush","controller","next","last","error","this","parse","e","Error","enqueue","objectMode","JSON","stringify","buffer","LtsvToJsonStream","options","instance","transform","call","flush","createLtsvToJsonStream","TransformStream"],"mappings":"AAiBA,SAASA,EAAWC,EAAeC,GACjC,MAAMC,EAAQC,OAAOH,GACfI,EAAQF,EAAMG,QAAQ,KAE5B,IAAe,IAAXD,EACF,UAAUE,8CAA8CJ,MAG1D,MAAMK,EAAQL,EAAMM,MAAM,EAAGJ,GACvBK,EAAQP,EAAMM,MAAMJ,EAAQ,GAElC,GAAIH,aCvBuBM,GAC3B,0BAA2BG,K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,OAAEA,GAAS,GAAUwC,EAEzCC,EAAqC,CACzCH,OAAQ,GACRH,aACAJ,MAAO/B,EAASsB,EAAkBD,GAGpC,MAAO,CAOLqB,SAAAA,CACE3C,EACA2B,GAEAH,EAAKoB,KAAKF,EAAUA,EAASH,OAASvC,GAAO,EAAO2B,EACtD,EAMAkB,KAAAA,CACElB,GAEAH,EAAKoB,KAAKF,EAAUA,EAASH,QAAQ,EAAMZ,EAC7C,EAEJ,UAEgBmB,EACdL,GAEA,WAAWM,gBAAgBP,EAAiBC,GAC9C"} \ No newline at end of file +{"version":3,"file":"ltsv.modern.mjs","sources":["../../src/parser.ts","../../src/validator.ts","../../src/whatwg_stream.ts"],"sourcesContent":["import { isValidLabel, isValidValue } from './validator';\n\nimport { LtsvRecord } from './types';\n\ntype LtsvField = {\n label: string;\n value: string;\n};\n\n/**\n * split to label and value from field\n *\n * @private\n * @param chunk\n * @param strict\n * @throws {SyntaxError}\n * @throws {TypeError}\n */\nfunction splitField(chunk: string | undefined, strict: boolean): LtsvField {\n if (chunk === undefined) {\n throw new TypeError('chunk is undefined');\n }\n\n const index = chunk.indexOf(':');\n\n if (index === -1) {\n throw new SyntaxError(`field separator is not found: \"${chunk}\"`);\n }\n\n const label = chunk.slice(0, index);\n const value = chunk.slice(index + 1);\n\n if (strict && !isValidLabel(label)) {\n throw new SyntaxError(`unexpected character in label: \"${label}\"`);\n }\n\n if (strict && !isValidValue(value)) {\n throw new SyntaxError(`unexpected character in value: \"${value}\"`);\n }\n\n return {\n label,\n value\n };\n}\n\n/**\n * parse LTSV record\n *\n * @private\n * @param line\n * @param strict\n */\nfunction baseParseLine(line: string | undefined, strict: boolean): LtsvRecord {\n const fields = String(line)\n .replace(/(?:\\r?\\n)+$/, '')\n .split('\\t');\n\n const record: LtsvRecord = {};\n\n for (let i = 0, len = fields.length; i < len; ++i) {\n const { label, value } = splitField(fields[i], strict);\n\n record[label] = value;\n }\n\n return record;\n}\n\n/**\n * parse LTSV text\n *\n * @private\n * @param text\n * @param strict\n */\nfunction baseParse(text: string, strict: boolean): LtsvRecord[] {\n const lines = String(text)\n .replace(/(?:\\r?\\n)+$/, '')\n .split(/\\r?\\n/);\n\n const records: LtsvRecord[] = [];\n\n for (let i = 0, len = lines.length; i < len; ++i) {\n records[i] = baseParseLine(lines[i], strict);\n }\n\n return records;\n}\n\n/**\n * parse LTSV text\n *\n * @param text\n */\nexport function parse(text: string): LtsvRecord[] {\n return baseParse(text, false);\n}\n\n/**\n * parse LTSV record\n *\n * @param line\n */\nexport function parseLine(line: string): LtsvRecord {\n return baseParseLine(line, false);\n}\n\n/**\n * parse LTSV text\n *\n * @param text\n */\nexport function parseStrict(text: string): LtsvRecord[] {\n return baseParse(text, true);\n}\n\n/**\n * parse LTSV record\n *\n * @param line\n */\nexport function parseLineStrict(line: string): LtsvRecord {\n return baseParseLine(line, true);\n}\n","/**\n * validate label\n *\n * @param label\n */\nexport function isValidLabel(label: string): boolean {\n return /^[0-9A-Za-z_.-]+$/.test(label);\n}\n\n/**\n * validate for value\n *\n * @param value\n */\nexport function isValidValue(value: string): boolean {\n // eslint-disable-next-line no-control-regex\n return /^[\\x01-\\x08\\x0B\\x0C\\x0E-\\xFF]*$/.test(value);\n}\n","import { parseLine, parseLineStrict } from './parser';\nimport { LtsvRecord } from './types';\n\nexport type LtsvToJsonStreamOptions = {\n objectMode?: boolean;\n strict?: boolean;\n};\n\ntype LtsvToJsonStreamInstance = {\n buffer: string;\n objectMode: boolean;\n parse: typeof parseLine | typeof parseLineStrict;\n};\n\n/**\n * transform and push to stream\n *\n * @param text\n * @param isFlush\n * @param controller\n */\nfunction push(\n this: LtsvToJsonStreamInstance,\n text: string,\n isFlush: boolean,\n controller: TransformStreamDefaultController\n): void {\n let next = 0;\n let last = 0;\n let error: Error | null = null;\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n let index = text.indexOf('\\n', next);\n\n if (index === -1) {\n if (isFlush && next < text.length) {\n // NOTE: subtract 1 from text.length,\n // NOTE: because add 1 to index when slice.\n index = text.length - 1;\n } else {\n break;\n }\n }\n\n // NOTE: include `\\n`.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: -----------------|\n const line = text.slice(next, index + 1);\n\n let record: LtsvRecord = {};\n\n try {\n record = this.parse(line);\n } catch (e) {\n if (e instanceof Error) {\n error = e;\n }\n }\n\n if (error) {\n break;\n }\n\n controller.enqueue(this.objectMode ? record : JSON.stringify(record));\n\n // NOTE: save next start index.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: ------------------|\n last = next = index + 1;\n }\n\n this.buffer = text.slice(last);\n\n if (error) {\n controller.error(error);\n }\n}\n\n/**\n * LTSV to JSON transform stream\n *\n * @param options\n */\nexport function LtsvToJsonStream(\n options: LtsvToJsonStreamOptions = {\n objectMode: false,\n strict: false\n }\n): Transformer {\n const { objectMode = false, strict = false } = options;\n\n const instance: LtsvToJsonStreamInstance = {\n buffer: '',\n objectMode,\n parse: strict ? parseLineStrict : parseLine\n };\n\n return {\n /**\n * transform implementation.\n *\n * @param chunk\n * @param controller\n */\n transform(\n chunk: string,\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer + chunk, false, controller);\n },\n /**\n * flush implementation.\n *\n * @param controller\n */\n flush(\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer, true, controller);\n }\n };\n}\n\nexport function createLtsvToJsonStream(\n options?: LtsvToJsonStreamOptions\n): TransformStream {\n return new TransformStream(LtsvToJsonStream(options));\n}\n"],"names":["splitField","chunk","strict","undefined","TypeError","index","indexOf","SyntaxError","label","slice","value","test","isValidLabel","isValidValue","baseParseLine","line","fields","String","replace","split","record","i","len","length","parseLine","parseLineStrict","push","text","isFlush","controller","next","last","error","this","parse","e","Error","enqueue","objectMode","JSON","stringify","buffer","LtsvToJsonStream","options","instance","transform","call","flush","createLtsvToJsonStream","TransformStream"],"mappings":"AAkBA,SAASA,EAAWC,EAA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F,MAGFH,EAAWQ,QAAQJ,KAAKK,WAAalB,EAASmB,KAAKC,UAAUpB,IAK7DW,EAAOD,EAAOzB,EAAQ,EAGxB4B,KAAKQ,OAASd,EAAKlB,MAAMsB,GAErBC,GACFH,EAAWG,MAAMA,EAErB,UAOgBU,EACdC,EAAmC,CACjCL,YAAY,EACZpC,QAAQ,IAGV,MAAMoC,WAAEA,GAAa,EAAKpC,OAAEA,GAAS,GAAUyC,EAEzCC,EAAqC,CACzCH,OAAQ,GACRH,aACAJ,MAAOhC,EAASuB,EAAkBD,GAGpC,MAAO,CAOLqB,SAAAA,CACE5C,EACA4B,GAEAH,EAAKoB,KAAKF,EAAUA,EAASH,OAASxC,GAAO,EAAO4B,EACtD,EAMAkB,KAAAA,CACElB,GAEAH,EAAKoB,KAAKF,EAAUA,EAASH,QAAQ,EAAMZ,EAC7C,EAEJ,UAEgBmB,EACdL,GAEA,WAAWM,gBAAgBP,EAAiBC,GAC9C"} \ No newline at end of file diff --git a/dist/whatwg_stream/ltsv.module.mjs b/dist/whatwg_stream/ltsv.module.mjs index 20f756cb..6b9da9e4 100644 --- a/dist/whatwg_stream/ltsv.module.mjs +++ b/dist/whatwg_stream/ltsv.module.mjs @@ -1,2 +1,2 @@ -function r(r,e){var t=String(r),n=t.indexOf(":");if(-1===n)throw new SyntaxError('field separator is not found: "'+t+'"');var i=t.slice(0,n),o=t.slice(n+1);if(e&&!function(r){return/^[0-9A-Za-z_.-]+$/.test(r)}(i))throw new SyntaxError('unexpected character in label: "'+i+'"');if(e&&!function(r){return/^[\x01-\x08\x0B\x0C\x0E-\xFF]*$/.test(r)}(o))throw new SyntaxError('unexpected character in value: "'+o+'"');return{label:i,value:o}}function e(e,t){for(var n=String(e).replace(/(?:\r?\n)+$/,"").split("\t"),i={},o=0,a=n.length;o\n): void {\n let next = 0;\n let last = 0;\n let error: Error | null = null;\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n let index = text.indexOf('\\n', next);\n\n if (index === -1) {\n if (isFlush && next < text.length) {\n // NOTE: subtract 1 from text.length,\n // NOTE: because add 1 to index when slice.\n index = text.length - 1;\n } else {\n break;\n }\n }\n\n // NOTE: include `\\n`.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: -----------------|\n const line = text.slice(next, index + 1);\n\n let record: LtsvRecord = {};\n\n try {\n record = this.parse(line);\n } catch (e) {\n if (e instanceof Error) {\n error = e;\n }\n }\n\n if (error) {\n break;\n }\n\n controller.enqueue(this.objectMode ? record : JSON.stringify(record));\n\n // NOTE: save next start index.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: ------------------|\n last = next = index + 1;\n }\n\n this.buffer = text.slice(last);\n\n if (error) {\n controller.error(error);\n }\n}\n\n/**\n * LTSV to JSON transform stream\n *\n * @param options\n */\nexport function LtsvToJsonStream(\n options: LtsvToJsonStreamOptions = {\n objectMode: false,\n strict: false\n }\n): Transformer {\n const { objectMode = false, strict = false } = options;\n\n const instance: LtsvToJsonStreamInstance = {\n buffer: '',\n objectMode,\n parse: strict ? parseLineStrict : parseLine\n };\n\n return {\n /**\n * transform implementation.\n *\n * @param chunk\n * @param controller\n */\n transform(\n chunk: string,\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer + chunk, false, controller);\n },\n /**\n * flush implementation.\n *\n * @param controller\n */\n flush(\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer, true, controller);\n }\n };\n}\n\nexport function createLtsvToJsonStream(\n options?: LtsvToJsonStreamOptions\n): TransformStream {\n return new TransformStream(LtsvToJsonStream(options));\n}\n"],"names":["splitField","chunk","strict","field","String","index","indexOf","SyntaxError","label","slice","value","test","isValidLabel","isValidValue","baseParseLine","line","fields","replace","split","record","i","len","length","_splitField","parseLine","parseLineStrict","push","text","isFlush","controller","next","last","error","this","parse","e","Error","enqueue","objectMode","JSON","stringify","buffer","LtsvToJsonStream","options","_options$objectMode","_options$strict","instance","transform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G,GAIZ,GAAIH,EACF,MAGFH,EAAWQ,QAAQJ,KAAKK,WAAanB,EAASoB,KAAKC,UAAUrB,IAK7DY,EAAOD,EAAOzB,EAAQ,EAGxB4B,KAAKQ,OAASd,EAAKlB,MAAMsB,GAErBC,GACFH,EAAWG,MAAMA,EAErB,UAOgBU,EACdC,YAAAA,IAAAA,EAAmC,CACjCL,YAAY,EACZpC,QAAQ,IAGV,IAAsD0C,EAAPD,EAAvCL,WAAkBO,EAAqBF,EAAnBzC,OAEtB4C,EAAqC,CACzCL,OAAQ,GACRH,oBAJgBM,GAAQA,EAKxBV,eALgCW,GAAQA,EAKxBpB,EAAkBD,GAGpC,MAAO,CAOLuB,mBACE9C,EACA4B,GAEAH,EAAKsB,KAAKF,EAAUA,EAASL,OAASxC,GAAO,EAAO4B,EACtD,EAMAoB,eACEpB,GAEAH,EAAKsB,KAAKF,EAAUA,EAASL,QAAQ,EAAMZ,EAC7C,EAEJ,UAEgBqB,EACdP,GAEA,WAAWQ,gBAAgBT,EAAiBC,GAC9C"} \ No newline at end of file +{"version":3,"file":"ltsv.module.mjs","sources":["../../src/parser.ts","../../src/validator.ts","../../src/whatwg_stream.ts"],"sourcesContent":["import { isValidLabel, isValidValue } from './validator';\n\nimport { LtsvRecord } from './types';\n\ntype LtsvField = {\n label: string;\n value: string;\n};\n\n/**\n * split to label and value from field\n *\n * @private\n * @param chunk\n * @param strict\n * @throws {SyntaxError}\n * @throws {TypeError}\n */\nfunction splitField(chunk: string | undefined, strict: boolean): LtsvField {\n if (chunk === undefined) {\n throw new TypeError('chunk is undefined');\n }\n\n const index = chunk.indexOf(':');\n\n if (index === -1) {\n throw new SyntaxError(`field separator is not found: \"${chunk}\"`);\n }\n\n const label = chunk.slice(0, index);\n const value = chunk.slice(index + 1);\n\n if (strict && !isValidLabel(label)) {\n throw new SyntaxError(`unexpected character in label: \"${label}\"`);\n }\n\n if (strict && !isValidValue(value)) {\n throw new SyntaxError(`unexpected character in value: \"${value}\"`);\n }\n\n return {\n label,\n value\n };\n}\n\n/**\n * parse LTSV record\n *\n * @private\n * @param line\n * @param strict\n */\nfunction baseParseLine(line: string | undefined, strict: boolean): LtsvRecord {\n const fields = String(line)\n .replace(/(?:\\r?\\n)+$/, '')\n .split('\\t');\n\n const record: LtsvRecord = {};\n\n for (let i = 0, len = fields.length; i < len; ++i) {\n const { label, value } = splitField(fields[i], strict);\n\n record[label] = value;\n }\n\n return record;\n}\n\n/**\n * parse LTSV text\n *\n * @private\n * @param text\n * @param strict\n */\nfunction baseParse(text: string, strict: boolean): LtsvRecord[] {\n const lines = String(text)\n .replace(/(?:\\r?\\n)+$/, '')\n .split(/\\r?\\n/);\n\n const records: LtsvRecord[] = [];\n\n for (let i = 0, len = lines.length; i < len; ++i) {\n records[i] = baseParseLine(lines[i], strict);\n }\n\n return records;\n}\n\n/**\n * parse LTSV text\n *\n * @param text\n */\nexport function parse(text: string): LtsvRecord[] {\n return baseParse(text, false);\n}\n\n/**\n * parse LTSV record\n *\n * @param line\n */\nexport function parseLine(line: string): LtsvRecord {\n return baseParseLine(line, false);\n}\n\n/**\n * parse LTSV text\n *\n * @param text\n */\nexport function parseStrict(text: string): LtsvRecord[] {\n return baseParse(text, true);\n}\n\n/**\n * parse LTSV record\n *\n * @param line\n */\nexport function parseLineStrict(line: string): LtsvRecord {\n return baseParseLine(line, true);\n}\n","/**\n * validate label\n *\n * @param label\n */\nexport function isValidLabel(label: string): boolean {\n return /^[0-9A-Za-z_.-]+$/.test(label);\n}\n\n/**\n * validate for value\n *\n * @param value\n */\nexport function isValidValue(value: string): boolean {\n // eslint-disable-next-line no-control-regex\n return /^[\\x01-\\x08\\x0B\\x0C\\x0E-\\xFF]*$/.test(value);\n}\n","import { parseLine, parseLineStrict } from './parser';\nimport { LtsvRecord } from './types';\n\nexport type LtsvToJsonStreamOptions = {\n objectMode?: boolean;\n strict?: boolean;\n};\n\ntype LtsvToJsonStreamInstance = {\n buffer: string;\n objectMode: boolean;\n parse: typeof parseLine | typeof parseLineStrict;\n};\n\n/**\n * transform and push to stream\n *\n * @param text\n * @param isFlush\n * @param controller\n */\nfunction push(\n this: LtsvToJsonStreamInstance,\n text: string,\n isFlush: boolean,\n controller: TransformStreamDefaultController\n): void {\n let next = 0;\n let last = 0;\n let error: Error | null = null;\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n let index = text.indexOf('\\n', next);\n\n if (index === -1) {\n if (isFlush && next < text.length) {\n // NOTE: subtract 1 from text.length,\n // NOTE: because add 1 to index when slice.\n index = text.length - 1;\n } else {\n break;\n }\n }\n\n // NOTE: include `\\n`.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: -----------------|\n const line = text.slice(next, index + 1);\n\n let record: LtsvRecord = {};\n\n try {\n record = this.parse(line);\n } catch (e) {\n if (e instanceof Error) {\n error = e;\n }\n }\n\n if (error) {\n break;\n }\n\n controller.enqueue(this.objectMode ? record : JSON.stringify(record));\n\n // NOTE: save next start index.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: ------------------|\n last = next = index + 1;\n }\n\n this.buffer = text.slice(last);\n\n if (error) {\n controller.error(error);\n }\n}\n\n/**\n * LTSV to JSON transform stream\n *\n * @param options\n */\nexport function LtsvToJsonStream(\n options: LtsvToJsonStreamOptions = {\n objectMode: false,\n strict: false\n }\n): Transformer {\n const { objectMode = false, strict = false } = options;\n\n const instance: LtsvToJsonStreamInstance = {\n buffer: '',\n objectMode,\n parse: strict ? parseLineStrict : parseLine\n };\n\n return {\n /**\n * transform implementation.\n *\n * @param chunk\n * @param controller\n */\n transform(\n chunk: string,\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer + chunk, false, controller);\n },\n /**\n * flush implementation.\n *\n * @param controller\n */\n flush(\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer, true, controller);\n }\n };\n}\n\nexport function createLtsvToJsonStream(\n options?: LtsvToJsonStreamOptions\n): TransformStream {\n return new TransformStream(LtsvToJsonStream(options));\n}\n"],"names":["splitField","chunk","strict","undefined","TypeError","index","indexOf","SyntaxError","label","slice","value","test","isValidLabel","isValidValue","baseParseLine","line","fields","String","replace","split","record","i","len","length","_splitField","parseLine","parseLineStrict","push","text","isFlush","controller","next","last","error","this","parse","e","Error","enqueue","objectMode","JSON","stringify","buffer","LtsvToJsonStream","options","_options$objectMode","_options$strict","instance","transform","call","flush","createLtsvToJsonStream","TransformStream"],"mappings":"AAkBA,SAASA,EAAWC,EAA2BC,GAC7C,QAAcC,IAAVF,EACF,UAAUG,UAAU,sBAGtB,IAAMC,EAAQJ,EAAMK,QAAQ,KAE5B,IAAe,IAAXD,EACF,UAAUE,8CAA8CN,OAG1D,IAAMO,EAAQP,EAAMQ,MAAM,EAAGJ,GACvBK,EAAQT,EAAMQ,MAAMJ,EAAQ,GAElC,GAAIH,aC3BuBM,GAC3B,0BAA2BG,KAAKH,EAClC,CDyBiBI,CAAaJ,GAC1B,UAAUD,+CAA+CC,OAG3D,GAAIN,aCtBuBQ,GAE3B,wCAAyCC,KAAKD,EAChD,CDmBiBG,CAAaH,GAC1B,UAAUH,+CAA+CG,OAG3D,MAAO,CACLF,MAAAA,EACAE,MAAAA,EAEJ,CASA,SAASI,EAAcC,EAA0Bb,GAO/C,IANA,IAAMc,EAASC,OAAOF,GACnBG,QAAQ,cAAe,IACvBC,MAAM,MAEHC,EAAqB,GAElBC,EAAI,EAAGC,EAAMN,EAAOO,OAAQF,EAAIC,IAAOD,EAAG,CACjD,IAAAG,EAAyBxB,EAAWgB,EAAOK,GAAInB,GAE/CkB,EAFaI,EAALhB,OAAYgB,EAALd,MAKjB,OAAOU,CACT,UAqCgBK,EAAUV,GACxB,OAAOD,EAAcC,GAAM,EAC7B,UAgBgBW,EAAgBX,GAC9B,OAAOD,EAAcC,GAAM,EAC7B,CEvGA,SAASY,EAEPC,EACAC,EACAC,GAOA,IALA,IAAIC,EAAO,EACPC,EAAO,EACPC,EAAsB,OAGb,CACX,IAAI5B,EAAQuB,EAAKtB,QAAQ,KAAMyB,GAE/B,IAAe,IAAX1B,EAAc,CAChB,KAAIwB,GAAWE,EAAOH,EAAKL,QAKzB,MAFAlB,EAAQuB,EAAKL,OAAS,EAS1B,IAAMR,EAAOa,EAAKnB,MAAMsB,EAAM1B,EAAQ,GAElCe,EAAqB,GAEzB,IACEA,EAASc,KAAKC,MAAMpB,GACpB,MAAOqB,GACHA,aAAaC,QACfJ,EAAQG,GAIZ,GAAIH,EACF,MAGFH,EAAWQ,QAAQJ,KAAKK,WAAanB,EAASoB,KAAKC,UAAUrB,IAK7DY,EAAOD,EAAO1B,EAAQ,EAGxB6B,KAAKQ,OAASd,EAAKnB,MAAMuB,GAErBC,GACFH,EAAWG,MAAMA,EAErB,UAOgBU,EACdC,YAAAA,IAAAA,EAAmC,CACjCL,YAAY,EACZrC,QAAQ,IAGV,IAAsD2C,EAAPD,EAAvCL,WAAkBO,EAAqBF,EAAnB1C,OAEtB6C,EAAqC,CACzCL,OAAQ,GACRH,oBAJgBM,GAAQA,EAKxBV,eALgCW,GAAQA,EAKxBpB,EAAkBD,GAGpC,MAAO,CAOLuB,mBACE/C,EACA6B,GAEAH,EAAKsB,KAAKF,EAAUA,EAASL,OAASzC,GAAO,EAAO6B,EACtD,EAMAoB,eACEpB,GAEAH,EAAKsB,KAAKF,EAAUA,EAASL,QAAQ,EAAMZ,EAC7C,EAEJ,UAEgBqB,EACdP,GAEA,WAAWQ,gBAAgBT,EAAiBC,GAC9C"} \ No newline at end of file diff --git a/dist/whatwg_stream/ltsv.umd.js b/dist/whatwg_stream/ltsv.umd.js index 738afa39..6b944f83 100644 --- a/dist/whatwg_stream/ltsv.umd.js +++ b/dist/whatwg_stream/ltsv.umd.js @@ -1,2 +1,2 @@ -!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e||self).ltsv={})}(this,function(e){function r(e,r){var t=String(e),n=t.indexOf(":");if(-1===n)throw new SyntaxError('field separator is not found: "'+t+'"');var o=t.slice(0,n),i=t.slice(n+1);if(r&&!function(e){return/^[0-9A-Za-z_.-]+$/.test(e)}(o))throw new SyntaxError('unexpected character in label: "'+o+'"');if(r&&!function(e){return/^[\x01-\x08\x0B\x0C\x0E-\xFF]*$/.test(e)}(i))throw new SyntaxError('unexpected character in value: "'+i+'"');return{label:o,value:i}}function t(e,t){for(var n=String(e).replace(/(?:\r?\n)+$/,"").split("\t"),o={},i=0,f=n.length;i\n): void {\n let next = 0;\n let last = 0;\n let error: Error | null = null;\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n let index = text.indexOf('\\n', next);\n\n if (index === -1) {\n if (isFlush && next < text.length) {\n // NOTE: subtract 1 from text.length,\n // NOTE: because add 1 to index when slice.\n index = text.length - 1;\n } else {\n break;\n }\n }\n\n // NOTE: include `\\n`.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: -----------------|\n const line = text.slice(next, index + 1);\n\n let record: LtsvRecord = {};\n\n try {\n record = this.parse(line);\n } catch (e) {\n if (e instanceof Error) {\n error = e;\n }\n }\n\n if (error) {\n break;\n }\n\n controller.enqueue(this.objectMode ? record : JSON.stringify(record));\n\n // NOTE: save next start index.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: ------------------|\n last = next = index + 1;\n }\n\n this.buffer = text.slice(last);\n\n if (error) {\n controller.error(error);\n }\n}\n\n/**\n * LTSV to JSON transform stream\n *\n * @param options\n */\nexport function LtsvToJsonStream(\n options: LtsvToJsonStreamOptions = {\n objectMode: false,\n strict: false\n }\n): Transformer {\n const { objectMode = false, strict = false } = options;\n\n const instance: LtsvToJsonStreamInstance = {\n buffer: '',\n objectMode,\n parse: strict ? parseLineStrict : parseLine\n };\n\n return {\n /**\n * transform implementation.\n *\n * @param chunk\n * @param controller\n */\n transform(\n chunk: string,\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer + chunk, false, controller);\n },\n /**\n * flush implementation.\n *\n * @param controller\n */\n flush(\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer, true, controller);\n }\n };\n}\n\nexport function createLtsvToJsonStream(\n options?: LtsvToJsonStreamOptions\n): TransformStream {\n return new TransformStream(LtsvToJsonStream(options));\n}\n"],"names":["splitField","chunk","strict","field","String","index","indexOf","SyntaxError","label","slice","value","test","isValidLabel","isValidValue","baseParseLine","line","fields","replace","split","record","i","len","length","_splitField","parseLine","parseLineStrict","push","text","isFlush","controller","next","last","error","this","parse","e","Error","enqueue","objectMode","JSON","stringify","buffer","LtsvToJsonStream","options","_options$objectMode","_options$strict","instance","transform","call","flush","TransformStream"],"mappings":"8NAiBA,SAASA,EAAWC,EAAeC,GACjC,IAAMC,EAAQC,OAAOH,GACfI,EAAQF,EAAMG,QAAQ,KAE5B,IAAe,IAAXD,EACF,UAAUE,8CAA8CJ,OAG1D,IAAMK,EAAQL,EAAMM,MAAM,EAAGJ,GACvBK,EAAQP,EAAMM,MAAMJ,EAAQ,GAElC,GAAIH,aCvBuBM,GAC3B,0BAA2BG,KAAKH,EAClC,CDqBiBI,CAAaJ,GAC1B,UAAUD,+CAA+CC,OAG3D,GAAIN,aClBuBQ,GAE3B,wCAAyCC,KAAKD,EAChD,CDeiBG,CAAaH,GAC1B,UAAUH,+CAA+CG,OAG3D,MAAO,CACLF,MAAAA,EACAE,MAAAA,EAEJ,CASA,SAASI,EAAcC,EAAcb,GAOnC,IANA,IAAMc,EAASZ,OAAOW,GACnBE,QAAQ,cAAe,IACvBC,MAAM,MAEHC,EAAqB,GAElBC,EAAI,EAAGC,EAAML,EAAOM,OAAQF,EAAIC,IAAOD,EAAG,CACjD,IAAAG,EAAyBvB,EAAWgB,EAAOI,GAAIlB,GAE/CiB,EAFaI,EAALf,OAAYe,EAALb,MAKjB,OAAOS,CACT,UAqCgBK,EAAUT,GACxB,OAAOD,EAAcC,GAAM,EAC7B,UAgBgBU,EAAgBV,GAC9B,OAAOD,EAAcC,GAAM,EAC7B,CEnGA,SAASW,EAEPC,EACAC,EACAC,GAOA,IALA,IAAIC,EAAO,EACPC,EAAO,EACPC,EAAsB,OAGb,CACX,IAAI3B,EAAQsB,EAAKrB,QAAQ,KAAMwB,GAE/B,IAAe,IAAXzB,EAAc,CAChB,KAAIuB,GAAWE,EAAOH,EAAKL,QAKzB,MAFAjB,EAAQsB,EAAKL,OAAS,EAS1B,IAAMP,EAAOY,EAAKlB,MAAMqB,EAAMzB,EAAQ,GAElCc,EAAqB,GAEzB,IACEA,EAASc,KAAKC,MAAMnB,GACpB,MAAOoB,GACHA,aAAaC,QACfJ,EAAQG,GAIZ,GAAIH,EACF,MAGFH,EAAWQ,QAAQJ,KAAKK,WAAanB,EAASoB,KAAKC,UAAUrB,IAK7DY,EAAOD,EAAOzB,EAAQ,EAGxB4B,KAAKQ,OAASd,EAAKlB,MAAMsB,GAErBC,GACFH,EAAWG,MAAMA,EAErB,UAOgBU,EACdC,YAAAA,IAAAA,EAAmC,CACjCL,YAAY,EACZpC,QAAQ,IAGV,IAAsD0C,EAAPD,EAAvCL,WAAkBO,EAAqBF,EAAnBzC,OAEtB4C,EAAqC,CACzCL,OAAQ,GACRH,oBAJgBM,GAAQA,EAKxBV,eALgCW,GAAQA,EAKxBpB,EAAkBD,GAGpC,MAAO,CAOLuB,mBACE9C,EACA4B,GAEAH,EAAKsB,KAAKF,EAAUA,EAASL,OAASxC,GAAO,EAAO4B,EACtD,EAMAoB,eACEpB,GAEAH,EAAKsB,KAAKF,EAAUA,EAASL,QAAQ,EAAMZ,EAC7C,EAEJ,wDAGEc,GAEA,WAAWO,gBAAgBR,EAAiBC,GAC9C"} \ No newline at end of file +{"version":3,"file":"ltsv.umd.js","sources":["../../src/parser.ts","../../src/validator.ts","../../src/whatwg_stream.ts"],"sourcesContent":["import { isValidLabel, isValidValue } from './validator';\n\nimport { LtsvRecord } from './types';\n\ntype LtsvField = {\n label: string;\n value: string;\n};\n\n/**\n * split to label and value from field\n *\n * @private\n * @param chunk\n * @param strict\n * @throws {SyntaxError}\n * @throws {TypeError}\n */\nfunction splitField(chunk: string | undefined, strict: boolean): LtsvField {\n if (chunk === undefined) {\n throw new TypeError('chunk is undefined');\n }\n\n const index = chunk.indexOf(':');\n\n if (index === -1) {\n throw new SyntaxError(`field separator is not found: \"${chunk}\"`);\n }\n\n const label = chunk.slice(0, index);\n const value = chunk.slice(index + 1);\n\n if (strict && !isValidLabel(label)) {\n throw new SyntaxError(`unexpected character in label: \"${label}\"`);\n }\n\n if (strict && !isValidValue(value)) {\n throw new SyntaxError(`unexpected character in value: \"${value}\"`);\n }\n\n return {\n label,\n value\n };\n}\n\n/**\n * parse LTSV record\n *\n * @private\n * @param line\n * @param strict\n */\nfunction baseParseLine(line: string | undefined, strict: boolean): LtsvRecord {\n const fields = String(line)\n .replace(/(?:\\r?\\n)+$/, '')\n .split('\\t');\n\n const record: LtsvRecord = {};\n\n for (let i = 0, len = fields.length; i < len; ++i) {\n const { label, value } = splitField(fields[i], strict);\n\n record[label] = value;\n }\n\n return record;\n}\n\n/**\n * parse LTSV text\n *\n * @private\n * @param text\n * @param strict\n */\nfunction baseParse(text: string, strict: boolean): LtsvRecord[] {\n const lines = String(text)\n .replace(/(?:\\r?\\n)+$/, '')\n .split(/\\r?\\n/);\n\n const records: LtsvRecord[] = [];\n\n for (let i = 0, len = lines.length; i < len; ++i) {\n records[i] = baseParseLine(lines[i], strict);\n }\n\n return records;\n}\n\n/**\n * parse LTSV text\n *\n * @param text\n */\nexport function parse(text: string): LtsvRecord[] {\n return baseParse(text, false);\n}\n\n/**\n * parse LTSV record\n *\n * @param line\n */\nexport function parseLine(line: string): LtsvRecord {\n return baseParseLine(line, false);\n}\n\n/**\n * parse LTSV text\n *\n * @param text\n */\nexport function parseStrict(text: string): LtsvRecord[] {\n return baseParse(text, true);\n}\n\n/**\n * parse LTSV record\n *\n * @param line\n */\nexport function parseLineStrict(line: string): LtsvRecord {\n return baseParseLine(line, true);\n}\n","/**\n * validate label\n *\n * @param label\n */\nexport function isValidLabel(label: string): boolean {\n return /^[0-9A-Za-z_.-]+$/.test(label);\n}\n\n/**\n * validate for value\n *\n * @param value\n */\nexport function isValidValue(value: string): boolean {\n // eslint-disable-next-line no-control-regex\n return /^[\\x01-\\x08\\x0B\\x0C\\x0E-\\xFF]*$/.test(value);\n}\n","import { parseLine, parseLineStrict } from './parser';\nimport { LtsvRecord } from './types';\n\nexport type LtsvToJsonStreamOptions = {\n objectMode?: boolean;\n strict?: boolean;\n};\n\ntype LtsvToJsonStreamInstance = {\n buffer: string;\n objectMode: boolean;\n parse: typeof parseLine | typeof parseLineStrict;\n};\n\n/**\n * transform and push to stream\n *\n * @param text\n * @param isFlush\n * @param controller\n */\nfunction push(\n this: LtsvToJsonStreamInstance,\n text: string,\n isFlush: boolean,\n controller: TransformStreamDefaultController\n): void {\n let next = 0;\n let last = 0;\n let error: Error | null = null;\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n let index = text.indexOf('\\n', next);\n\n if (index === -1) {\n if (isFlush && next < text.length) {\n // NOTE: subtract 1 from text.length,\n // NOTE: because add 1 to index when slice.\n index = text.length - 1;\n } else {\n break;\n }\n }\n\n // NOTE: include `\\n`.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: -----------------|\n const line = text.slice(next, index + 1);\n\n let record: LtsvRecord = {};\n\n try {\n record = this.parse(line);\n } catch (e) {\n if (e instanceof Error) {\n error = e;\n }\n }\n\n if (error) {\n break;\n }\n\n controller.enqueue(this.objectMode ? record : JSON.stringify(record));\n\n // NOTE: save next start index.\n // NOTE: foo:foo\\tbar:bar\\nfoo:foo\\tbar:bar\\n\n // NOTE: ------------------|\n last = next = index + 1;\n }\n\n this.buffer = text.slice(last);\n\n if (error) {\n controller.error(error);\n }\n}\n\n/**\n * LTSV to JSON transform stream\n *\n * @param options\n */\nexport function LtsvToJsonStream(\n options: LtsvToJsonStreamOptions = {\n objectMode: false,\n strict: false\n }\n): Transformer {\n const { objectMode = false, strict = false } = options;\n\n const instance: LtsvToJsonStreamInstance = {\n buffer: '',\n objectMode,\n parse: strict ? parseLineStrict : parseLine\n };\n\n return {\n /**\n * transform implementation.\n *\n * @param chunk\n * @param controller\n */\n transform(\n chunk: string,\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer + chunk, false, controller);\n },\n /**\n * flush implementation.\n *\n * @param controller\n */\n flush(\n controller: TransformStreamDefaultController\n ): void {\n push.call(instance, instance.buffer, true, controller);\n }\n };\n}\n\nexport function createLtsvToJsonStream(\n options?: LtsvToJsonStreamOptions\n): TransformStream {\n return new TransformStream(LtsvToJsonStream(options));\n}\n"],"names":["splitField","chunk","strict","undefined","TypeError","index","indexOf","SyntaxError","label","slice","value","test","isValidLabel","isValidValue","baseParseLine","line","fields","String","replace","split","record","i","len","length","_splitField","parseLine","parseLineStrict","push","text","isFlush","controller","next","last","error","this","parse","e","Error","enqueue","objectMode","JSON","stringify","buffer","LtsvToJsonStream","options","_options$objectMode","_options$strict","instance","transform","call","flush","TransformStream"],"mappings":"8NAkBA,SAASA,EAAWC,EAA2BC,GAC7C,QAAcC,IAAVF,EACF,UAAUG,UAAU,sBAGtB,IAAMC,EAAQJ,EAAMK,QAAQ,KAE5B,IAAe,IAAXD,EACF,UAAUE,8CAA8CN,OAG1D,IAAMO,EAAQP,EAAMQ,MAAM,EAAGJ,GACvBK,EAAQT,EAAMQ,MAAMJ,EAAQ,GAElC,GAAIH,aC3BuBM,GAC3B,0BAA2BG,KAAKH,EAClC,CDyBiBI,CAAaJ,GAC1B,UAAUD,+CAA+CC,OAG3D,GAAIN,aCtBuBQ,GAE3B,wCAAyCC,KAAKD,EAChD,CDmBiBG,CAAaH,GAC1B,UAAUH,+CAA+CG,OAG3D,MAAO,CACLF,MAAAA,EACAE,MAAAA,EAEJ,CASA,SAASI,EAAcC,EAA0Bb,GAO/C,IANA,IAAMc,EAASC,OAAOF,GAC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AAKF,EAAUA,EAASL,QAAQ,EAAMZ,EAC7C,EAEJ,wDAGEc,GAEA,WAAWO,gBAAgBR,EAAiBC,GAC9C"} \ No newline at end of file diff --git a/test/formatter.ts b/test/formatter.ts index fb27e8ee..8f8809cc 100644 --- a/test/formatter.ts +++ b/test/formatter.ts @@ -1,4 +1,4 @@ -import assert = require('assert'); +import { strict as assert } from 'node:assert'; import { format, formatStrict } from '../src/formatter'; diff --git a/test/nodejs_stream.ts b/test/nodejs_stream.ts index 557847a8..3798ee99 100644 --- a/test/nodejs_stream.ts +++ b/test/nodejs_stream.ts @@ -1,8 +1,13 @@ -import assert = require('assert'); -import * as fs from 'fs'; +import { strict as assert } from 'node:assert'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import * as url from 'node:url'; import { createLtsvToJsonStream } from '../src/nodejs_stream'; +const __filename = url.fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + describe('nodejs_stream', function () { describe('#write() and #end()', function () { it('should convert to JSON from LTSV', function (done) { @@ -120,7 +125,9 @@ describe('nodejs_stream', function () { done(); }); - fs.createReadStream(require.resolve('./log/valid-1.ltsv')).pipe(stream); + fs.createReadStream(path.join(__dirname, './log/valid-1.ltsv')).pipe( + stream + ); }); it('should convert LTSV log of 3 line', function (done) { @@ -144,7 +151,9 @@ describe('nodejs_stream', function () { done(); }); - fs.createReadStream(require.resolve('./log/valid-3.ltsv')).pipe(stream); + fs.createReadStream(path.join(__dirname, './log/valid-3.ltsv')).pipe( + stream + ); }); it('should throw error if invalid LTSV log', function (done) { @@ -156,7 +165,9 @@ describe('nodejs_stream', function () { done(); }); - fs.createReadStream(require.resolve('./log/invalid.ltsv')).pipe(stream); + fs.createReadStream(path.join(__dirname, './log/invalid.ltsv')).pipe( + stream + ); }); }); }); diff --git a/test/parser.ts b/test/parser.ts index a1343dfd..ab7a34cf 100644 --- a/test/parser.ts +++ b/test/parser.ts @@ -1,4 +1,4 @@ -import assert = require('assert'); +import { strict as assert } from 'node:assert'; import { parse, parseLine, parseStrict, parseLineStrict } from '../src/parser'; diff --git a/test/validator.ts b/test/validator.ts index 59dc3fc5..53d5ce2a 100644 --- a/test/validator.ts +++ b/test/validator.ts @@ -1,4 +1,4 @@ -import assert = require('assert'); +import { strict as assert } from 'node:assert'; import { isValidLabel, isValidValue } from '../src/validator'; @@ -52,7 +52,13 @@ describe('validator', function () { ).split(''); for (let i = 0, len = unassertedCharacters.length; i < len; ++i) { - assert(isValidLabel(unassertedCharacters[i]) === false); + const character = unassertedCharacters[i]; + + if (!character) { + assert.fail(); + } + + assert(isValidLabel(character) === false); } }); }); @@ -84,7 +90,13 @@ describe('validator', function () { ).split(''); for (let i = 0, len = assertedCharacters.length; i < len; ++i) { - assert(isValidValue(assertedCharacters[i]) === true); + const character = assertedCharacters[i]; + + if (!character) { + assert.fail(); + } + + assert(isValidValue(character) === true); } }); diff --git a/test/whatwg_stream.ts b/test/whatwg_stream.ts index f400aeac..713a82a2 100644 --- a/test/whatwg_stream.ts +++ b/test/whatwg_stream.ts @@ -1,4 +1,4 @@ -import assert = require('assert'); +import { strict as assert } from 'node:assert'; import { createLtsvToJsonStream } from '../src/whatwg_stream';