Skip to content

Commit fc02b00

Browse files
authored
Implementing 'tiny-strings' (#2)
1 parent b5199a2 commit fc02b00

13 files changed

+47
-121
lines changed

dist/tiny-jsonl.cjs

+6-27
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,21 @@
33
*
44
* @copyright 2024 Jason Mulligan <[email protected]>
55
* @license BSD-3-Clause
6-
* @version 1.0.3
6+
* @version 1.0.4
77
*/
88
'use strict';
99

10+
var tinyStrings = require('tiny-strings');
11+
1012
const STRING_NEW_LINE = "\n";
1113
const STRING_REPLACEMENT = "$1 ";
12-
const STRING_STRING = "string";
1314
const STRING_OBJECT = "object";
1415
const MSG_INVALID_INPUT = "Argument must be an Array or Object";
1516

1617
function rewrite (arg = "") {
1718
return arg.replace(/"/g, "\\\"");
1819
}
1920

20-
function crawl (arg = {}, strings = []) {
21-
const keys = Object.keys(arg);
22-
strings.push(...keys);
23-
for (const key of strings) {
24-
if (typeof arg[key] === STRING_STRING) {
25-
strings.push(rewrite(arg[key]));
26-
} else if (Array.isArray(arg[key])) {
27-
for (const value of arg[key]) {
28-
if (typeof value === STRING_OBJECT) {
29-
crawl(value, strings);
30-
} else if (typeof value === STRING_STRING) {
31-
strings.push(rewrite(value));
32-
}
33-
}
34-
} else if (typeof arg[key] === STRING_OBJECT) {
35-
crawl(arg[key], strings);
36-
}
37-
}
38-
}
39-
4021
function jsonl (arg) {
4122
if (typeof arg !== STRING_OBJECT) {
4223
throw new TypeError(MSG_INVALID_INPUT);
@@ -48,17 +29,15 @@ function jsonl (arg) {
4829
result = arg.map(i => jsonl(i)).join(STRING_NEW_LINE);
4930
} else {
5031
let tmp = JSON.stringify(arg, null, 0);
51-
const strings = [];
52-
53-
crawl(arg, strings);
32+
const extracted = tinyStrings.strings(arg, true).map(rewrite);
5433

55-
for (const [idx, val] of strings.entries()) {
34+
for (const [idx, val] of extracted.entries()) {
5635
tmp = tmp.replace(`"${val}"`, `INDEX_${idx}`);
5736
}
5837

5938
result = tmp.replace(/(:|,)/g, STRING_REPLACEMENT);
6039

61-
for (const [idx, val] of strings.entries()) {
40+
for (const [idx, val] of extracted.entries()) {
6241
result = result.replace(`INDEX_${idx}`, `"${val}"`);
6342
}
6443
}

dist/tiny-jsonl.js

+5-26
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,13 @@
33
*
44
* @copyright 2024 Jason Mulligan <[email protected]>
55
* @license BSD-3-Clause
6-
* @version 1.0.3
6+
* @version 1.0.4
77
*/
8-
const STRING_NEW_LINE = "\n";
8+
import {strings}from'tiny-strings';const STRING_NEW_LINE = "\n";
99
const STRING_REPLACEMENT = "$1 ";
10-
const STRING_STRING = "string";
1110
const STRING_OBJECT = "object";
1211
const MSG_INVALID_INPUT = "Argument must be an Array or Object";function rewrite (arg = "") {
1312
return arg.replace(/"/g, "\\\"");
14-
}function crawl (arg = {}, strings = []) {
15-
const keys = Object.keys(arg);
16-
strings.push(...keys);
17-
for (const key of strings) {
18-
if (typeof arg[key] === STRING_STRING) {
19-
strings.push(rewrite(arg[key]));
20-
} else if (Array.isArray(arg[key])) {
21-
for (const value of arg[key]) {
22-
if (typeof value === STRING_OBJECT) {
23-
crawl(value, strings);
24-
} else if (typeof value === STRING_STRING) {
25-
strings.push(rewrite(value));
26-
}
27-
}
28-
} else if (typeof arg[key] === STRING_OBJECT) {
29-
crawl(arg[key], strings);
30-
}
31-
}
3213
}function jsonl (arg) {
3314
if (typeof arg !== STRING_OBJECT) {
3415
throw new TypeError(MSG_INVALID_INPUT);
@@ -40,17 +21,15 @@ const MSG_INVALID_INPUT = "Argument must be an Array or Object";function rewrite
4021
result = arg.map(i => jsonl(i)).join(STRING_NEW_LINE);
4122
} else {
4223
let tmp = JSON.stringify(arg, null, 0);
43-
const strings = [];
44-
45-
crawl(arg, strings);
24+
const extracted = strings(arg, true).map(rewrite);
4625

47-
for (const [idx, val] of strings.entries()) {
26+
for (const [idx, val] of extracted.entries()) {
4827
tmp = tmp.replace(`"${val}"`, `INDEX_${idx}`);
4928
}
5029

5130
result = tmp.replace(/(:|,)/g, STRING_REPLACEMENT);
5231

53-
for (const [idx, val] of strings.entries()) {
32+
for (const [idx, val] of extracted.entries()) {
5433
result = result.replace(`INDEX_${idx}`, `"${val}"`);
5534
}
5635
}

dist/tiny-jsonl.min.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
22
2024 Jason Mulligan <[email protected]>
3-
@version 1.0.3
3+
@version 1.0.4
44
*/
5-
const e="string",r="object";function t(e=""){return e.replace(/"/g,'\\"')}function o(n={},f=[]){const s=Object.keys(n);f.push(...s);for(const s of f)if(typeof n[s]===e)f.push(t(n[s]));else if(Array.isArray(n[s]))for(const c of n[s])typeof c===r?o(c,f):typeof c===e&&f.push(t(c));else typeof n[s]===r&&o(n[s],f)}function n(e){if(typeof e!==r)throw new TypeError("Argument must be an Array or Object");let t;if(Array.isArray(e))t=e.map((e=>n(e))).join("\n");else{let r=JSON.stringify(e,null,0);const n=[];o(e,n);for(const[e,t]of n.entries())r=r.replace(`"${t}"`,`INDEX_${e}`);t=r.replace(/(:|,)/g,"$1 ");for(const[e,r]of n.entries())t=t.replace(`INDEX_${e}`,`"${r}"`)}return t}export{n as jsonl};//# sourceMappingURL=tiny-jsonl.min.js.map
5+
import{strings as r}from"tiny-strings";function e(r=""){return r.replace(/"/g,'\\"')}function t(n){if("object"!=typeof n)throw new TypeError("Argument must be an Array or Object");let o;if(Array.isArray(n))o=n.map((r=>t(r))).join("\n");else{let t=JSON.stringify(n,null,0);const i=r(n,!0).map(e);for(const[r,e]of i.entries())t=t.replace(`"${e}"`,`INDEX_${r}`);o=t.replace(/(:|,)/g,"$1 ");for(const[r,e]of i.entries())o=o.replace(`INDEX_${r}`,`"${e}"`)}return o}export{t as jsonl};//# sourceMappingURL=tiny-jsonl.min.js.map

dist/tiny-jsonl.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tiny-jsonl.umd.js

+5-26
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,13 @@
33
*
44
* @copyright 2024 Jason Mulligan <[email protected]>
55
* @license BSD-3-Clause
6-
* @version 1.0.3
6+
* @version 1.0.4
77
*/
8-
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?f(exports):typeof define==='function'&&define.amd?define(['exports'],f):(g=typeof globalThis!=='undefined'?globalThis:g||self,f(g.lru={}));})(this,(function(exports){'use strict';const STRING_NEW_LINE = "\n";
8+
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?f(exports,require('tiny-strings')):typeof define==='function'&&define.amd?define(['exports','tiny-strings'],f):(g=typeof globalThis!=='undefined'?globalThis:g||self,f(g.jsonl={},g.tinyStrings));})(this,(function(exports,tinyStrings){'use strict';const STRING_NEW_LINE = "\n";
99
const STRING_REPLACEMENT = "$1 ";
10-
const STRING_STRING = "string";
1110
const STRING_OBJECT = "object";
1211
const MSG_INVALID_INPUT = "Argument must be an Array or Object";function rewrite (arg = "") {
1312
return arg.replace(/"/g, "\\\"");
14-
}function crawl (arg = {}, strings = []) {
15-
const keys = Object.keys(arg);
16-
strings.push(...keys);
17-
for (const key of strings) {
18-
if (typeof arg[key] === STRING_STRING) {
19-
strings.push(rewrite(arg[key]));
20-
} else if (Array.isArray(arg[key])) {
21-
for (const value of arg[key]) {
22-
if (typeof value === STRING_OBJECT) {
23-
crawl(value, strings);
24-
} else if (typeof value === STRING_STRING) {
25-
strings.push(rewrite(value));
26-
}
27-
}
28-
} else if (typeof arg[key] === STRING_OBJECT) {
29-
crawl(arg[key], strings);
30-
}
31-
}
3213
}function jsonl (arg) {
3314
if (typeof arg !== STRING_OBJECT) {
3415
throw new TypeError(MSG_INVALID_INPUT);
@@ -40,17 +21,15 @@ const MSG_INVALID_INPUT = "Argument must be an Array or Object";function rewrite
4021
result = arg.map(i => jsonl(i)).join(STRING_NEW_LINE);
4122
} else {
4223
let tmp = JSON.stringify(arg, null, 0);
43-
const strings = [];
44-
45-
crawl(arg, strings);
24+
const extracted = tinyStrings.strings(arg, true).map(rewrite);
4625

47-
for (const [idx, val] of strings.entries()) {
26+
for (const [idx, val] of extracted.entries()) {
4827
tmp = tmp.replace(`"${val}"`, `INDEX_${idx}`);
4928
}
5029

5130
result = tmp.replace(/(:|,)/g, STRING_REPLACEMENT);
5231

53-
for (const [idx, val] of strings.entries()) {
32+
for (const [idx, val] of extracted.entries()) {
5433
result = result.replace(`INDEX_${idx}`, `"${val}"`);
5534
}
5635
}

dist/tiny-jsonl.umd.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tiny-jsonl.umd.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)