From 57aa0410062f123f459faa4870ede5bea62f0bf2 Mon Sep 17 00:00:00 2001 From: AOKI Yuto Date: Fri, 5 Jul 2024 00:48:25 +0900 Subject: [PATCH] export --- build.ts | 22 +++++++++++++++ lib/define.js | 75 +++++++++++++++++++++++++++++++-------------------- lib/format.js | 34 ++++++++++++++--------- 3 files changed, 89 insertions(+), 42 deletions(-) diff --git a/build.ts b/build.ts index 2eeb060..03d7ed1 100644 --- a/build.ts +++ b/build.ts @@ -46,6 +46,28 @@ const files = [ .replace(/export \{[^}]+\};$/m, '') .trim(), ], + [ + 'lib/define.js', + ( + await ( + await Bun.build({ + entrypoints: ['define.ts'], + minify: false, + }) + ).outputs[0].text() + ) + ], + [ + 'lib/format.js', + ( + await ( + await Bun.build({ + entrypoints: ['format.ts'], + minify: false, + }) + ).outputs[0].text() + ) + ], [ 'lib/index.js', ( diff --git a/lib/define.js b/lib/define.js index 2fa3dce..fc9084c 100644 --- a/lib/define.js +++ b/lib/define.js @@ -1,33 +1,50 @@ -function defineID(s) { - const i = []; - for (let r = 0;r < s.format.length; r++) { - const t = s.format[r], n = t.characters.split(""), e = t.repeats || 1; - for (let o = 0;o < e; o++) - i.push(n); +// define.ts +function defineID(options) { + const sequence = []; + for (let i = 0;i < options.format.length; i++) { + const f = options.format[i]; + const chars = f.characters.split(""); + const repeats = f.repeats || 1; + for (let j = 0;j < repeats; j++) { + sequence.push(chars); + } } - const a = () => { - return s.format.map((r) => { - return r.characters.length ** (r.repeats || 1); - }).reduce((r, t) => r * t, 1); - }, c = i.map((r, t) => i.slice(t).reduce((n, e) => n * e.length, 1)); - return { n: a, stringify: (r) => { - if (r < 0) + const n = () => { + return options.format.map((p) => { + return p.characters.length ** (p.repeats || 1); + }).reduce((a, b) => a * b, 1); + }; + const orders = sequence.map((_, i) => sequence.slice(i).reduce((a, b) => a * b.length, 1)); + const stringify = (num) => { + if (num < 0) { throw new Error("negative number is not acceptable."); - if (a() < r + 1) - throw new Error(`number of ids exceeded. number of available ids = ${a()}`); - return c.map((n, e) => Math.floor(Number(r % n / (c[e + 1] || 1)))).map((n, e) => i[e][n]).join(""); - }, parse: (r) => { - if (r.length !== i.length) + } + if (n() < num + 1) { + throw new Error(`number of ids exceeded. number of available ids = ${n()}`); + } + const digits = orders.map((o, i) => Math.floor(Number(num % o / (orders[i + 1] || 1)))); + return digits.map((d, i) => sequence[i][d]).join(""); + }; + const parse = (id) => { + if (id.length !== sequence.length) { throw new Error("invalid id length."); - let t; - const n = r.split("").map((e, o) => { - const f = i[o], m = f.indexOf(e); - if (m < 0) - t = new Error(`invalid id. '${e}' (index = ${o}) is not acceptable in [${f.join(", ")}].`); - return m * (c[o + 1] || 1); + } + let error; + const digits = id.split("").map((c, i) => { + const pattern = sequence[i]; + const n2 = pattern.indexOf(c); + if (n2 < 0) { + error = new Error(`invalid id. '${c}' (index = ${i}) is not acceptable in [${pattern.join(", ")}].`); + } + return n2 * (orders[i + 1] || 1); }); - if (t) - throw t; - return n.reduce((e, o) => e + o, 0); - } }; -} \ No newline at end of file + if (error) { + throw error; + } + return digits.reduce((total, n2) => total + n2, 0); + }; + return { n, stringify, parse }; +} +export { + defineID +}; diff --git a/lib/format.js b/lib/format.js index 583fed2..1ea63ee 100644 --- a/lib/format.js +++ b/lib/format.js @@ -1,17 +1,25 @@ -function createFormat(n, c, o) { - const t = n.split("").map((r) => { +// format.ts +function createFormat(template, definitions, compress) { + const format = template.split("").map((f) => { return { - characters: c[r] || r + characters: definitions[f] || f }; }); - if (!o) - return t; - return t.reduceRight((r, e) => { - const a = r[0]; - if (r.length > 0 && a.characters === e.characters) - return r[0] = Object.assign({}, e, { - repeats: (e.repeats || 1) + (a.repeats || 1) - }), r; - return r.unshift(e), r; + if (!compress) { + return format; + } + return format.reduceRight((list, pattern) => { + const head = list[0]; + if (list.length > 0 && head.characters === pattern.characters) { + list[0] = Object.assign({}, pattern, { + repeats: (pattern.repeats || 1) + (head.repeats || 1) + }); + return list; + } + list.unshift(pattern); + return list; }, []); -} \ No newline at end of file +} +export { + createFormat +};