├── .gitignore ├── tsconfig.json ├── tslint.json ├── package.json ├── .vscode └── launch.json ├── README.md ├── lib └── guid.ts └── tests └── guid.spec.ts /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es2015", 4 | "module": "commonjs", 5 | "outDir": "dist", 6 | "declaration": true, 7 | "declarationMap": true, 8 | "sourceMap": true, 9 | "strict": true, 10 | "esModuleInterop": true 11 | } 12 | } -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint:recommended", 3 | "rules": { 4 | "max-line-length": { 5 | "options": [400] 6 | }, 7 | "new-parens": true, 8 | "no-arg": true, 9 | "eofline": false, 10 | "no-trailing-whitespace": false, 11 | "no-bitwise": true, 12 | "no-empty-interface": false, 13 | "no-conditional-assignment": true, 14 | "no-consecutive-blank-lines": false, 15 | "variable-name": [true, "ban-keywords", "check-format", "allow-leading-underscore"], 16 | "no-console": { 17 | "severity": "warning", 18 | "options": [ 19 | "debug", 20 | "info", 21 | "log", 22 | "time", 23 | "timeEnd", 24 | "trace" 25 | ] 26 | } 27 | }, 28 | "jsRules": { 29 | "max-line-length": { 30 | "options": [120] 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "guid-typescript", 3 | "version": "1.0.8", 4 | "description": "Guid generator to typescript", 5 | "scripts": { 6 | "verify": ".\\node_modules\\.bin\\tslint .\\lib\\**", 7 | "test": "npm run verify && mocha -r ts-node/register tests/*.spec.ts", 8 | "build": ".\\node_modules\\.bin\\tsc lib/guid -t es3 -m commonjs -d --outDir dist" 9 | }, 10 | "author": "nicolas", 11 | "license": "ISC", 12 | "keywords": [ 13 | "typescript", 14 | "guid", 15 | "identifier" 16 | ], 17 | "main": "./dist/guid.js", 18 | "types": "./dist/guid.d.ts", 19 | "repository": { 20 | "type": "git", 21 | "url": "https://github.com/NicolasDeveloper/guid-typescript" 22 | }, 23 | "devDependencies": { 24 | "@types/chai": "^4.1.0", 25 | "@types/mocha": "^2.2.46", 26 | "@types/node": "^9.3.0", 27 | "chai": "^4.1.2", 28 | "mocha": "^4.1.0", 29 | "ts-node": "^7.0.1", 30 | "tslint": "^5.8.0", 31 | "typescript": "^3.1.3" 32 | }, 33 | "dependencies": {}, 34 | "files": [ 35 | "/dist/" 36 | ] 37 | } 38 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "Test", 9 | "type": "node", 10 | "request": "launch", 11 | "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", 12 | "stopOnEntry": false, 13 | "args": [ 14 | "--require", 15 | "ts-node/register", 16 | "tests/*.spec.ts" 17 | ], 18 | "cwd": "${workspaceRoot}", 19 | "preLaunchTask": null, 20 | "runtimeExecutable": null, 21 | "runtimeArgs": [ 22 | "--nolazy" 23 | ], 24 | "env": { 25 | "NODE_ENV": "development" 26 | }, 27 | "externalConsole": false, 28 | "sourceMaps": true, 29 | "outDir": null 30 | } 31 | ] 32 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Guid Typescript 2 | 3 | Guid Typescript is a library that lets you generate guid code 4 | 5 | ## Installation and usage 6 | ### Installation 7 | 8 | ``` 9 | npm i guid-typescript --save 10 | ``` 11 | 12 | ### Basic usage 13 | 14 | ```typescript 15 | import { Guid } from "guid-typescript"; 16 | 17 | export class Example { 18 | public id: Guid; 19 | constructor() { 20 | this.id = Guid.create(); // ==> b77d409a-10cd-4a47-8e94-b0cd0ab50aa1 21 | } 22 | } 23 | ``` 24 | 25 | ## Props and Methods 26 | 27 | | Method/Prop | Description | Test | Status | 28 | |---|---|---|---| 29 | | static isGuid (guid: any): boolean | Check if value is a guid code | OK | Ready | 30 | | static create ( ): Guid | Create a new guid | OK | Ready | 31 | | static createEmpty ( ): Guid | Create an empty guid | OK | Ready | 32 | | static parse (guid: string): Guid | Creates a guid instance from a given guid as string | OK | Ready | 33 | | static raw ( ): string | Create a guid code in string format | OK | Ready | 34 | | equals (other: Guid): boolean | Compare a guid code | OK | Ready | 35 | | isEmpty ( ): boolean | Validate if a guid is empty | OK | Ready | 36 | | toString ( ): string | Parse a guid instance to string format | OK | Ready | 37 | | toJSON ( ): any | Parse to JSON format | OK | Ready | 38 | 39 | 40 | -------------------------------------------------------------------------------- /lib/guid.ts: -------------------------------------------------------------------------------- 1 | export class Guid { 2 | 3 | public static validator = new RegExp("^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$", "i"); 4 | 5 | public static EMPTY = "00000000-0000-0000-0000-000000000000"; 6 | 7 | public static isGuid(guid: any) { 8 | const value: string = guid.toString(); 9 | return guid && (guid instanceof Guid || Guid.validator.test(value)); 10 | } 11 | 12 | public static create(): Guid { 13 | return new Guid([Guid.gen(2), Guid.gen(1), Guid.gen(1), Guid.gen(1), Guid.gen(3)].join("-")); 14 | } 15 | 16 | public static createEmpty(): Guid { 17 | return new Guid("emptyguid"); 18 | } 19 | 20 | public static parse(guid: string): Guid { 21 | return new Guid(guid); 22 | } 23 | 24 | public static raw(): string { 25 | return [Guid.gen(2), Guid.gen(1), Guid.gen(1), Guid.gen(1), Guid.gen(3)].join("-"); 26 | } 27 | 28 | private static gen(count: number) { 29 | let out: string = ""; 30 | for (let i: number = 0; i < count; i++) { 31 | // tslint:disable-next-line:no-bitwise 32 | out += (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); 33 | } 34 | return out; 35 | } 36 | 37 | private value: string; 38 | 39 | private constructor(guid: string) { 40 | if (!guid) { throw new TypeError("Invalid argument; `value` has no value."); } 41 | 42 | this.value = Guid.EMPTY; 43 | 44 | if (guid && Guid.isGuid(guid)) { 45 | this.value = guid; 46 | } 47 | } 48 | 49 | public equals(other: Guid): boolean { 50 | // Comparing string `value` against provided `guid` will auto-call 51 | // toString on `guid` for comparison 52 | return Guid.isGuid(other) && this.value === other.toString(); 53 | } 54 | 55 | public isEmpty(): boolean { 56 | return this.value === Guid.EMPTY; 57 | } 58 | 59 | public toString(): string { 60 | return this.value; 61 | } 62 | 63 | public toJSON(): any { 64 | return { 65 | value: this.value, 66 | }; 67 | } 68 | } 69 | 70 | 71 | -------------------------------------------------------------------------------- /tests/guid.spec.ts: -------------------------------------------------------------------------------- 1 | import { assert, expect } from "chai"; 2 | import "mocha"; 3 | import { Guid } from "../lib/guid"; 4 | 5 | describe("Guid test", () => { 6 | 7 | it("Should create a guid", () => { 8 | const wrong = "wrongguid"; 9 | expect(Guid.isGuid(wrong)).equal(false); 10 | 11 | const right = Guid.create(); 12 | expect(Guid.isGuid(right)).equal(true); 13 | }); 14 | 15 | it("Should raw a guid", () => { 16 | const wrong = "wrongguid"; 17 | expect(Guid.isGuid(wrong)).equal(false); 18 | 19 | const right = Guid.raw(); 20 | expect(Guid.isGuid(right)).equal(true); 21 | }); 22 | 23 | it("Should compare another guid", () => { 24 | const wrong = Guid.create(); 25 | expect(wrong.equals(Guid.create())).equal(false); 26 | 27 | const right = Guid.create(); 28 | expect(right.equals(right)).equal(true); 29 | }); 30 | 31 | it("Should compare another guid empty", () => { 32 | const wrong = Guid.createEmpty(); 33 | expect(wrong.equals(Guid.create())).equal(false); 34 | 35 | const right = Guid.createEmpty(); 36 | expect(right.equals(Guid.createEmpty())).equal(true); 37 | }); 38 | 39 | it("Should verify if is guid", () => { 40 | const wrong = "wrong guid"; 41 | expect(Guid.isGuid(wrong)).equal(false); 42 | 43 | const right = Guid.create(); 44 | expect(Guid.isGuid(right)).equal(true); 45 | }); 46 | 47 | it("Should parse a guid", () => { 48 | const wrong = Guid.raw(); 49 | expect(Guid.parse(wrong).equals(Guid.create())).equal(false); 50 | 51 | const right = Guid.raw(); 52 | expect(Guid.parse(right).equals(Guid.parse(right))).equal(true); 53 | }); 54 | 55 | it("Should be unique value", () => { 56 | const guids = []; 57 | for (let index = 0; index < 3000; index++) { 58 | guids.push(Guid.create()); 59 | } 60 | expect(guids.indexOf(guids[0]) < 0).equal(false); 61 | 62 | expect(guids.indexOf(Guid.create()) < 0).equal(true); 63 | }); 64 | 65 | }); --------------------------------------------------------------------------------