-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |