Skip to content

Commit

Permalink
speed up
Browse files Browse the repository at this point in the history
  • Loading branch information
uneco committed Jul 4, 2024
1 parent 04ec0ca commit 1450666
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 155 deletions.
58 changes: 33 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,45 @@

## dist/define.js
```js
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);
function defineID(a) {
const o = [];
for (let r = 0;r < a.format.length; r++) {
const e = a.format[r], t = e.characters.split(""), i = e.repeats || 1;
for (let n = 0;n < i; n++)
o.push(t);
}
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) => {
const l = a.format.map((r) => {
return r.characters.length ** (r.repeats || 1);
}).reduce((r, e) => r * e, 1), s = [];
for (let r = 0;r < o.length; r++)
s.push(o.slice(r).reduce((e, t) => e * t.length, 1));
return { n: l, stringify: (r) => {
if (r < 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("");
if (l < r + 1)
throw new Error(`number of ids exceeded. number of available ids = ${l}`);
let e = "";
for (let t = 0;t < s.length; t++) {
const i = s[t], n = Math.floor(r % i / (s[t + 1] || 1));
e += o[t][n];
}
return e;
}, parse: (r) => {
if (r.length !== i.length)
if (r.length !== o.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 e;
const t = r.split("").map((n, c) => {
const f = o[c], h = f.indexOf(n);
if (h < 0)
e = new Error(`invalid id. '${n}' (index = ${c}) is not acceptable in [${f.join(", ")}].`);
return h * (s[c + 1] || 1);
});
if (t)
throw t;
return n.reduce((e, o) => e + o, 0);
if (e)
throw e;
let i = 0;
for (let n = 0;n < t.length; n++)
i += t[n];
return i;
} };
}
```
Expand Down
50 changes: 33 additions & 17 deletions define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,45 @@ export function defineID(options: ConverterOptions) {
}
}

const n = (): number => {
return options.format
.map<number>((p) => {
return p.characters.length ** (p.repeats || 1);
})
.reduce((a, b) => a * b, 1);
}
const n = options.format
.map<number>((p) => {
return p.characters.length ** (p.repeats || 1);
})
.reduce((a, b) => a * b, 1);

const orders: number[] = []

const orders = sequence.map((_, i) => sequence.slice(i).reduce((a, b) => a * b.length, 1))
for (let i = 0; i < sequence.length; i++) {
orders.push(sequence.slice(i).reduce((a, b) => a * b.length, 1));
}

const stringify = (num: number): string => {
if (num < 0) {
throw new Error("negative number is not acceptable.");
}
if (n() < num + 1) {
if (n < num + 1) {
throw new Error(
`number of ids exceeded. number of available ids = ${n()}`,
`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("");
}

let buf = "";

for (let i = 0; i < orders.length; i++) {
const o = orders[i];
const d = Math.floor((num % o) / (orders[i + 1] || 1));
buf += sequence[i][d];
}

return buf;
};

const parse = (id: string): number => {
if (id.length !== sequence.length) {
throw new Error("invalid id length.");
}

let error: Error | undefined;
let error: Error | undefined;
const digits = id.split("").map((c, i) => {
const pattern = sequence[i];
const n = pattern.indexOf(c);
Expand All @@ -57,12 +67,18 @@ export function defineID(options: ConverterOptions) {
return n * (orders[i + 1] || 1);
});

if (error) {
if (error) {
throw error;
}

return digits.reduce((total, n) => total + n, 0);
}
let total = 0;

for (let i = 0; i < digits.length; i++) {
total += digits[i];
}

return total;
};

return { n, stringify, parse };
}
58 changes: 33 additions & 25 deletions dist/define.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
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);
function defineID(a) {
const o = [];
for (let r = 0;r < a.format.length; r++) {
const e = a.format[r], t = e.characters.split(""), i = e.repeats || 1;
for (let n = 0;n < i; n++)
o.push(t);
}
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) => {
const l = a.format.map((r) => {
return r.characters.length ** (r.repeats || 1);
}).reduce((r, e) => r * e, 1), s = [];
for (let r = 0;r < o.length; r++)
s.push(o.slice(r).reduce((e, t) => e * t.length, 1));
return { n: l, stringify: (r) => {
if (r < 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("");
if (l < r + 1)
throw new Error(`number of ids exceeded. number of available ids = ${l}`);
let e = "";
for (let t = 0;t < s.length; t++) {
const i = s[t], n = Math.floor(r % i / (s[t + 1] || 1));
e += o[t][n];
}
return e;
}, parse: (r) => {
if (r.length !== i.length)
if (r.length !== o.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 e;
const t = r.split("").map((n, c) => {
const f = o[c], h = f.indexOf(n);
if (h < 0)
e = new Error(`invalid id. '${n}' (index = ${c}) is not acceptable in [${f.join(", ")}].`);
return h * (s[c + 1] || 1);
});
if (t)
throw t;
return n.reduce((e, o) => e + o, 0);
if (e)
throw e;
let i = 0;
for (let n = 0;n < t.length; n++)
i += t[n];
return i;
} };
}
2 changes: 1 addition & 1 deletion lib/define.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export type ConverterOptions = {
format: Format;
};
export declare function defineID(options: ConverterOptions): {
n: () => number;
n: number;
stringify: (num: number) => string;
parse: (id: string) => number;
};
51 changes: 1 addition & 50 deletions lib/define.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 1 addition & 25 deletions lib/format.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 1 addition & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
// index.ts
import {defineID} from "./define";
import {createFormat} from "./format";
var id2 = {
defineID,
createFormat
};
var edge_id2_default = id2;
export {
edge_id2_default as default
};
import{defineID as o} from"./define";import{createFormat as r} from"./format";var t={defineID:o,createFormat:r},i=t;export{i as default};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.1",
"version": "0.0.3",
"name": "@kanamone/edge-id2",
"scripts": {
"prebuild": "bun run tsc --build",
Expand Down

0 comments on commit 1450666

Please sign in to comment.