Skip to content

Commit

Permalink
Introduce Backwards support for ES5 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
prokopschield authored Jun 15, 2022
1 parent c45d299 commit ca1bf8c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
26 changes: 14 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,39 @@ export type SunflakeConfig = EpochConfig & {
machineId?: bigint | number | string;
};

export const DEFAULT_EPOCH = 1_640_995_200_000n;
export const DEFAULT_EPOCH = BigInt(1_640_995_200_000);

export const generateSunflake = (
config?: SunflakeConfig
): ((time?: bigint | number) => string) => {
const machineId = BigInt(config?.machineId ?? 1) & 1023n;
const machineId = BigInt(config?.machineId ?? 1) & BigInt(1023);
const epoch = BigInt(config?.epoch ?? DEFAULT_EPOCH);

let lastTime = 0n;
let seq = 0n;
let lastTime = BigInt(0);
let seq = BigInt(0);

return (time: bigint | number = Date.now()) => {
// subtract epoch from received timestamp
let currentTime = BigInt(time) - epoch;

// generate sequence number
if (currentTime <= lastTime) {
if (seq < 4095n) {
if (seq < BigInt(4095)) {
currentTime = lastTime;
++seq;
} else {
currentTime = ++lastTime;
seq = 0n;
seq = BigInt(0);
}
} else {
lastTime = currentTime;
seq = 0n;
seq = BigInt(0);
}

// generate sunflake
return String((currentTime << 22n) | (machineId << 12n) | seq);
return String(
(currentTime << BigInt(22)) | (machineId << BigInt(12)) | seq
);
};
};

Expand All @@ -58,12 +60,12 @@ export const decode = (
) => {
const epoch = BigInt(config?.epoch || DEFAULT_EPOCH);
let snowflake = BigInt(sunflake);
const seq = snowflake & 4095n;
const seq = snowflake & BigInt(4095);

snowflake >>= 12n;
const machineId = snowflake & 1023n;
snowflake >>= BigInt(12);
const machineId = snowflake & BigInt(1023);

snowflake >>= 10n;
snowflake >>= BigInt(10);
const time = epoch + snowflake;

return { time, machineId, seq, epoch };
Expand Down
14 changes: 7 additions & 7 deletions tests/sunflake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ describe('Promise', () => {
});

expect(snowflake(some_constant)).toBe(
String((BigInt(some_constant) << 22n) + (1n << 12n))
String((BigInt(some_constant) << BigInt(22)) + (BigInt(1) << BigInt(12)))
);
});

describe('decode() tests', () => {
it('runs with custom input', () => {
const epoch = 1_739_461_378n;
const machineID = 420n;
const time = 38_735_781_431n;
const epoch = BigInt(1_739_461_378);
const machineID = BigInt(420);
const time = BigInt(38_735_781_431);
const config: SunflakeConfig = { epoch, machineId: machineID };
const sunflake = generateSunflake(config);
const snowflake = sunflake(time);
Expand All @@ -88,7 +88,7 @@ describe('Promise', () => {
expect(parsed.epoch).toBe(config.epoch);
expect(parsed.machineId).toBe(config.machineId);
expect(parsed.time).toBe(time);
expect(parsed.seq).toBe(0n);
expect(parsed.seq).toBe(BigInt(0));
});
it('runs with defaults', () => {
const now = Date.now();
Expand All @@ -97,8 +97,8 @@ describe('Promise', () => {
const { epoch, machineId, seq, time } = decode(snowflake);

expect(epoch).toBe(DEFAULT_EPOCH);
expect(machineId).toBe(1n);
expect(seq).toBe(0n);
expect(machineId).toBe(BigInt(1));
expect(seq).toBe(BigInt(0));
expect(time).toBe(BigInt(now));
});
});
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"moduleResolution": "node",
"target": "ES2020",
"target": "ES5",
"module": "commonjs",
"lib": ["es2015", "es2016", "es2017", "es2020"],
"strict": true,
Expand Down

0 comments on commit ca1bf8c

Please sign in to comment.