Skip to content

Commit

Permalink
refactor(strings): replace hex formatters w/ thi.ng/hex
Browse files Browse the repository at this point in the history
- add thi.ng/hex dependency
- replace U8/16/24/32/64
- refactor uuid()
  • Loading branch information
postspectacular committed Nov 13, 2020
1 parent b31c872 commit 6d7446c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
1 change: 1 addition & 0 deletions packages/strings/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"dependencies": {
"@thi.ng/api": "^6.13.1",
"@thi.ng/errors": "^1.2.22",
"@thi.ng/hex": "^0.0.1",
"@thi.ng/memoize": "^2.1.3"
},
"files": [
Expand Down
25 changes: 18 additions & 7 deletions packages/strings/src/radix.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import {
U16 as $16,
U24 as $24,
U32 as $32,
U64HL,
U8 as $8,
} from "@thi.ng/hex";
import { memoizeJ } from "@thi.ng/memoize";
import type { Stringer } from "./api";
import { repeat } from "./repeat";

/**
* Returns a {@link Stringer} which formats given numbers to `radix`, `len`
* and with optional prefix (not included in `len`).
* Returns a {@link Stringer} which formats given numbers to `radix`, `len` and
* with optional prefix (not included in `len`).
*
* @remarks
* Only bases 2 - 36 are supported, due to native `Number.toString()`
* limitations.
*
* @param radix -
* @param len -
Expand Down Expand Up @@ -43,28 +54,28 @@ export const B32 = radix(2, 32);
* 8bit hex conversion preset.
* Assumes unsigned inputs.
*/
export const U8 = radix(16, 2);
export const U8 = $8;

/**
* 16bit hex conversion preset.
* Assumes unsigned inputs.
*/
export const U16 = radix(16, 4);
export const U16 = $16;

/**
* 24bit hex conversion preset.
* Assumes unsigned inputs.
*/
export const U24 = radix(16, 6);
export const U24 = $24;

/**
* 32bit hex conversion preset.
* Assumes unsigned inputs.
*/
export const U32 = radix(16, 8);
export const U32 = $32;

/**
* 64bit hex conversion preset (2x 32bit ints)
* Assumes unsigned inputs.
*/
export const U64 = (hi: number, lo: number) => U32(hi) + U32(lo);
export const U64 = U64HL;
40 changes: 15 additions & 25 deletions packages/strings/src/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
import { U8 } from "./radix";
import { U16BE, U32BE, U48BE } from "@thi.ng/hex";

/**
* Returns UUID formatted string of given byte array from optional start
* index `i` (default: 0). Array must have min. length 16.
*
* @param id
* @param i
* @remarks
* This is the same function as {@link @thi.ng/random#uuid}.
*
* @param id -
* @param i -
*/
export const uuid = (id: ArrayLike<number>, i = 0) =>
[
U8(id[i++]),
U8(id[i++]),
U8(id[i++]),
U8(id[i++]),
"-",
U8(id[i++]),
U8(id[i++]),
"-",
U8(id[i++]),
U8(id[i++]),
"-",
U8(id[i++]),
U8(id[i++]),
"-",
U8(id[i++]),
U8(id[i++]),
U8(id[i++]),
U8(id[i++]),
U8(id[i++]),
U8(id[i++]),
].join("");
U32BE(id, i) +
"-" +
U16BE(id, i + 4) +
"-" +
U16BE(id, i + 6) +
"-" +
U16BE(id, i + 8) +
"-" +
U48BE(id, i + 10);

0 comments on commit 6d7446c

Please sign in to comment.