Skip to content

Commit

Permalink
export
Browse files Browse the repository at this point in the history
  • Loading branch information
uneco committed Jul 4, 2024
1 parent 9e8b019 commit 57aa041
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 42 deletions.
22 changes: 22 additions & 0 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
(
Expand Down
75 changes: 46 additions & 29 deletions lib/define.js
Original file line number Diff line number Diff line change
@@ -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);
} };
}
if (error) {
throw error;
}
return digits.reduce((total, n2) => total + n2, 0);
};
return { n, stringify, parse };
}
export {
defineID
};
34 changes: 21 additions & 13 deletions lib/format.js
Original file line number Diff line number Diff line change
@@ -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;
}, []);
}
}
export {
createFormat
};

0 comments on commit 57aa041

Please sign in to comment.