Skip to content

Commit 6865a0a

Browse files
committed
feat(core): add Uid primitive and its tests
1 parent 9682d97 commit 6865a0a

File tree

7 files changed

+126
-0
lines changed

7 files changed

+126
-0
lines changed

packages/core/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../jest.project')({ dirname: __dirname });

packages/core/package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "@dgraphium/core",
3+
"license": "MIT",
4+
"version": "0.0.0",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"files": [
8+
"dist/*"
9+
],
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/binier/dgraphium.git",
13+
"directory": "packages/core"
14+
},
15+
"publishConfig": {
16+
"access": "public"
17+
},
18+
"scripts": {
19+
"clean:dist": "rimraf dist",
20+
"compile": "tsc -b tsconfig.build.json",
21+
"build": "yarn clean:dist && yarn compile",
22+
"test": "jest --coverage",
23+
"test:watch": "jest --coverage --watch",
24+
"test:prod": "yarn test -- --no-cache",
25+
"report-coverage": "cat ./coverage/lcov.info | coveralls",
26+
"prepublishOnly": "yarn build"
27+
},
28+
"devDependencies": {
29+
"@types/jest": "^25.2.1",
30+
"@types/node": "^13.13.0",
31+
"coveralls": "^3.0.2",
32+
"jest": "^25.4.0",
33+
"rimraf": "^3.0.2",
34+
"ts-jest": "^25.4.0",
35+
"ts-node": "^8.8.2",
36+
"typescript": "^3.0.3"
37+
}
38+
}

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './uid';

packages/core/src/uid.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export type UidLike = Uid | string | number;
2+
3+
const uidRegex = new RegExp(/^(0x)?[0-9a-f]{1,16}$/, 'i');
4+
5+
export function isValidUid(uid: UidLike) {
6+
switch (typeof uid) {
7+
case 'string': return uidRegex.test(uid);
8+
case 'number': return uid > 0 && Number.isSafeInteger(uid);
9+
default: return false;
10+
}
11+
}
12+
13+
export class Uid {
14+
private val: string;
15+
16+
constructor(uid: UidLike) {
17+
if (uid instanceof Uid)
18+
return new Uid(uid.val);
19+
20+
if (!isValidUid(uid))
21+
throw Error('invalid_uid');
22+
23+
if (typeof uid === 'number')
24+
this.val = '0x' + uid.toString(16);
25+
else
26+
this.val = uid.startsWith('0x') ? uid : ('0x' + uid);
27+
}
28+
29+
toString() {
30+
return this.val;
31+
}
32+
}

packages/core/test/uid.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Uid, UidLike, isValidUid } from '../src/uid';
2+
3+
const attachValOnFail = (uid: UidLike, fn: () => any) => {
4+
try { fn(); }
5+
catch (err) {
6+
err.message = `${err.message}\n\nPassed Uid: ${uid}`;
7+
throw err;
8+
}
9+
};
10+
11+
const shouldBeInvalid = (val: UidLike) => attachValOnFail(val, () => {
12+
expect(isValidUid(val)).toBeFalsy();
13+
expect(() => new Uid(val)).toThrow();
14+
});
15+
16+
const shouldBeValid = (val: UidLike) => attachValOnFail(val, () => {
17+
expect(isValidUid(val)).toBeTruthy();
18+
expect(() => new Uid(val)).not.toThrow();
19+
});
20+
21+
describe('Uid test', () => {
22+
it('should throw if uid: number and is negative', () => {
23+
shouldBeInvalid(-1);
24+
});
25+
26+
it('should throw if uid: string and is invalid', () => {
27+
shouldBeInvalid('0xg');
28+
shouldBeInvalid('xf');
29+
shouldBeInvalid('');
30+
shouldBeInvalid(' ');
31+
});
32+
33+
it('should allow uid: string without "0x" prefix', () => {
34+
shouldBeValid('0');
35+
shouldBeValid('f');
36+
});
37+
});

packages/core/tsconfig.build.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../../tsconfig.build.json",
3+
4+
"compilerOptions": {
5+
"outDir": "./dist"
6+
},
7+
8+
"include": [
9+
"src/**/*"
10+
]
11+
}

packages/core/tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
4+
"compilerOptions": {
5+
}
6+
}

0 commit comments

Comments
 (0)