File tree 7 files changed +126
-0
lines changed 7 files changed +126
-0
lines changed Original file line number Diff line number Diff line change
1
+ module . exports = require ( '../../jest.project' ) ( { dirname : __dirname } ) ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ export * from './uid' ;
Original file line number Diff line number Diff line change
1
+ export type UidLike = Uid | string | number ;
2
+
3
+ const uidRegex = new RegExp ( / ^ ( 0 x ) ? [ 0 - 9 a - 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
+ }
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "extends" : " ../../tsconfig.build.json" ,
3
+
4
+ "compilerOptions" : {
5
+ "outDir" : " ./dist"
6
+ },
7
+
8
+ "include" : [
9
+ " src/**/*"
10
+ ]
11
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "extends" : " ../../tsconfig.json" ,
3
+
4
+ "compilerOptions" : {
5
+ }
6
+ }
You can’t perform that action at this time.
0 commit comments