Skip to content

Commit 8298d5a

Browse files
committed
Fixed falsy test to exclude 0
1 parent 1cd18e6 commit 8298d5a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/generateUniqueID.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import isFalsy from './isFalsy'
2+
13
const { Snowflake } = require('../build/Release/snowflake');
24

35
const CUSTOM_EPOCH = 1546300800000; // 01-01-2019
@@ -14,6 +16,8 @@ const initConfig: Config = {
1416
returnNumber: false
1517
}
1618

19+
20+
1721
/**
1822
* Constructs a UniqueID object which stores method for generation
1923
* of a unique 64 bit time sortable id and a method for retreiving
@@ -40,7 +44,9 @@ export class UniqueID {
4044

4145
// A 12 bit machine id, if not passed in then a random id will be used
4246
// Ternary operator was used to make sure "0" isn't considered to be falsy.
43-
this._MACHINE_ID = config.machineID !== undefined ? config.machineID : Math.floor(Math.random() * MAX_MACHINE_ID);
47+
this._MACHINE_ID = (
48+
!isFalsy(config.machineID) ? config.machineID : Math.floor(Math.random() * MAX_MACHINE_ID)
49+
) as number;
4450

4551
// Check if the number is satisfies all the conditions
4652
if (!Number.isInteger(this._MACHINE_ID)) throw Error("Machine Id should be a decimal number");

src/isFalsy.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default (value: any) => {
2+
if (value === undefined) return true;
3+
if (isNaN(value)) return true;
4+
if (Array.isArray(value) && !value.length) return true;
5+
if (value === "") return true;
6+
if (value === null) return true;
7+
}

0 commit comments

Comments
 (0)