spliterator 3.2.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,22 +6,24 @@
6
6
  import { type PathLike, WriteStream } from "node:fs";
7
7
  /**
8
8
  * Callback for writing a line to a newline-delimited file.
9
+ *
10
+ * The trailing newline is appended for you — pass the content alone.
9
11
  */
10
12
  export interface WriteLineCallback {
11
13
  /**
12
- * Write a line to the file. The line should not contain a newline character.
14
+ * Write a line to the file, terminated with a newline. The content should not contain one itself.
13
15
  */
14
16
  (content: string): Promise<void>;
15
17
  /**
16
- * Write a chunk to the file. The chunk should not contain a newline character.
18
+ * Write a chunk to the file, terminated with a newline. The chunk should not contain one itself.
17
19
  */
18
20
  (chunk: any, encoding: BufferEncoding): Promise<void>;
19
21
  }
20
22
  export interface NewlineWriter extends AsyncDisposable {
21
23
  /**
22
- * Write a line to the file.
24
+ * Write a line to the file, terminated with a newline.
23
25
  *
24
- * The content should not contain a newline character.
26
+ * The content should not contain a newline character — the writer supplies the delimiter.
25
27
  */
26
28
  write: WriteLineCallback;
27
29
  dispose: () => Promise<void>;
@@ -29,6 +31,15 @@ export interface NewlineWriter extends AsyncDisposable {
29
31
  }
30
32
  /**
31
33
  * Creates a writer for newline-delimited files.
34
+ *
35
+ * Every {@linkcode NewlineWriter.write} terminates its content with `\n`, so the file is delimited (and round-trips
36
+ * through {@linkcode TextSpliterator}) without the caller appending anything:
37
+ *
38
+ * ```ts
39
+ * await using out = createNewlineWriter("rows.jsonl")
40
+ *
41
+ * for (const row of rows) await out.write(JSON.stringify(row))
42
+ * ```
32
43
  */
33
44
  export declare function createNewlineWriter(filePath: PathLike): NewlineWriter;
34
45
  //# sourceMappingURL=writer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"writer.d.ts","sourceRoot":"","sources":["../../lib/writer.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAqB,KAAK,QAAQ,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAEvE;;GAEG;AACH,MAAM,WAAW,iBAAiB;IACjC;;OAEG;IACH,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAChC;;OAEG;IACH,CAAC,KAAK,EAAE,GAAG,EAAE,QAAQ,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACrD;AAED,MAAM,WAAW,aAAc,SAAQ,eAAe;IACrD;;;;OAIG;IACH,KAAK,EAAE,iBAAiB,CAAA;IACxB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5B,MAAM,EAAE,WAAW,CAAA;CACnB;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,GAAG,aAAa,CAqCrE"}
1
+ {"version":3,"file":"writer.d.ts","sourceRoot":"","sources":["../../lib/writer.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAqB,KAAK,QAAQ,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAEvE;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IACjC;;OAEG;IACH,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAChC;;OAEG;IACH,CAAC,KAAK,EAAE,GAAG,EAAE,QAAQ,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACrD;AAED,MAAM,WAAW,aAAc,SAAQ,eAAe;IACrD;;;;OAIG;IACH,KAAK,EAAE,iBAAiB,CAAA;IACxB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5B,MAAM,EAAE,WAAW,CAAA;CACnB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,GAAG,aAAa,CA6CrE"}
package/out/lib/writer.js CHANGED
@@ -6,18 +6,34 @@
6
6
  import { createWriteStream, WriteStream } from "node:fs";
7
7
  /**
8
8
  * Creates a writer for newline-delimited files.
9
+ *
10
+ * Every {@linkcode NewlineWriter.write} terminates its content with `\n`, so the file is delimited (and round-trips
11
+ * through {@linkcode TextSpliterator}) without the caller appending anything:
12
+ *
13
+ * ```ts
14
+ * await using out = createNewlineWriter("rows.jsonl")
15
+ *
16
+ * for (const row of rows) await out.write(JSON.stringify(row))
17
+ * ```
9
18
  */
10
19
  export function createNewlineWriter(filePath) {
11
20
  const writer = createWriteStream(filePath);
12
- const write = (content) => {
21
+ const write = (content, encoding) => {
13
22
  return new Promise((resolve, reject) => {
14
- writer.write(content, "utf8", (error) => {
23
+ const settle = (error) => {
15
24
  if (error) {
16
25
  reject(error);
17
26
  return;
18
27
  }
19
28
  resolve();
20
- });
29
+ };
30
+ // The delimiter goes out as its own chunk rather than concatenated onto `content`: the
31
+ // two-argument overload accepts a Buffer, and `buffer + "\n"` would stringify it via
32
+ // `toString()` and corrupt the bytes. Stream writes are ordered, so the newline always
33
+ // follows its content, and settling on the delimiter's callback resolves once the whole
34
+ // line has flushed.
35
+ writer.write(content, encoding ?? "utf8");
36
+ writer.write("\n", "utf8", settle);
21
37
  });
22
38
  };
23
39
  const dispose = () => {
@@ -1 +1 @@
1
- {"version":3,"file":"writer.js","sourceRoot":"","sources":["../../lib/writer.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,iBAAiB,EAAiB,WAAW,EAAE,MAAM,SAAS,CAAA;AA2BvE;;GAEG;AACH,MAAM,UAAU,mBAAmB,CAAC,QAAkB;IACrD,MAAM,MAAM,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IAE1C,MAAM,KAAK,GAAsB,CAAC,OAAO,EAAE,EAAE;QAC5C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACtC,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;gBACvC,IAAI,KAAK,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AAuF;YACvF,wFAAwF;YACxF,oBAAoB;YACpB,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,QAAQ,IAAI,MAAM,CAAC,CAAA;YACzC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;QACnC,CAAC,CAAC,CAAA;IACH,CAAC,CAAA;IAED,MAAM,OAAO,GAAG,GAAG,EAAE;QACpB,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC5C,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;gBACtB,IAAI,KAAK,EAAE,CAAC;oBACX,MAAM,CAAC,KAAK,CAAC,CAAA;oBAEb,OAAM;gBACP,CAAC;gBAED,OAAO,EAAE,CAAA;YACV,CAAC,CAAC,CAAA;QACH,CAAC,CAAC,CAAA;IACH,CAAC,CAAA;IAED,OAAO;QACN,KAAK;QACL,MAAM;QACN,OAAO;QACP,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,OAAO;KAC9B,CAAA;AACF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spliterator",
3
- "version": "3.2.0",
3
+ "version": "4.0.0",
4
4
  "description": "Line-delimited iterator library for CSV, NDJSON, and other delimited formats.",
5
5
  "keywords": [
6
6
  "csv",