Skip to content

Commit

Permalink
chore: remove unused agility, and related tests, from encodeTime() #13 (
Browse files Browse the repository at this point in the history
#14)

closes #13 closes #14
  • Loading branch information
grempe authored Feb 13, 2022
1 parent ea72adb commit 61ea4c2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/ulid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ function validateTimestamp(timestamp: number): void {
}
}

function encodeTime(timestamp: number, len: number): string {
function encodeTime(timestamp: number): string {
validateTimestamp(timestamp);

let mod: number;
let str: string = "";

for (let currentLen: number = len; currentLen > 0; currentLen--) {
for (let tLen: number = TIME_LEN; tLen > 0; tLen--) {
mod = timestamp % ENCODING_LEN;
str = ENCODING.charAt(mod) + str;
timestamp = (timestamp - mod) / ENCODING_LEN;
Expand Down Expand Up @@ -159,12 +159,12 @@ export const ulidFactory = (args?: ULIDFactoryArgs): ULIDFactory => {
lastTime = timestampOrNow;
const random = encodeRandom(RANDOM_LEN);
lastRandom = random;
return encodeTime(timestampOrNow, TIME_LEN) + random;
return encodeTime(timestampOrNow) + random;
} else {
// <= lastTime : increment lastRandom
const random = incrementBase32(lastRandom);
lastRandom = random;
return encodeTime(lastTime, TIME_LEN) + random;
return encodeTime(lastTime) + random;
}
};
})();
Expand All @@ -173,7 +173,7 @@ export const ulidFactory = (args?: ULIDFactoryArgs): ULIDFactory => {
return function (timestamp?: number): ULID {
let timestampOrNow: number = timestamp || Date.now();
validateTimestamp(timestampOrNow);
return encodeTime(timestampOrNow, TIME_LEN) + encodeRandom(RANDOM_LEN);
return encodeTime(timestampOrNow) + encodeRandom(RANDOM_LEN);
};
})();
}
Expand Down
16 changes: 4 additions & 12 deletions test/ulid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,13 @@ describe("ulid", function () {

describe("encodeTime", function () {
it("should return expected encoded result", function () {
expect(encodeTime(1469918176385, 10)).to.equal("01ARYZ6S41");
});

it("should change length properly", function () {
expect(encodeTime(1470264322240, 12)).to.equal("0001AS99AA60");
});

it("should truncate time if not enough length", function () {
expect(encodeTime(1470118279201, 8)).to.equal("AS4Y1E11");
expect(encodeTime(TEST_TIME_EPOCH_MS)).to.equal(TEST_TIME_ENCODED);
});

describe("should throw an error", function () {
it("if validateTimestamp is being called", function () {
expect(() => {
encodeTime(Math.pow(2, 48), 8);
encodeTime(Math.pow(2, 48));
}).to.throw(/cannot encode a timestamp larger than/);
});
});
Expand Down Expand Up @@ -225,7 +217,7 @@ describe("ulid", function () {
describe("should throw an error", function () {
it("if validateTimestamp is being called", function () {
expect(() => {
encodeTime(Math.pow(2, 48), 8);
encodeTime(Math.pow(2, 48));
}).to.throw(/cannot encode a timestamp larger than/);
});
});
Expand Down Expand Up @@ -307,7 +299,7 @@ describe("ulid", function () {
describe("should throw an error", function () {
it("if validateTimestamp is being called", function () {
expect(() => {
encodeTime(Math.pow(2, 48), 8);
encodeTime(Math.pow(2, 48));
}).to.throw(/cannot encode a timestamp larger than/);
});
});
Expand Down

0 comments on commit 61ea4c2

Please sign in to comment.