Skip to content
This repository has been archived by the owner on Jun 17, 2020. It is now read-only.

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
avmaisak committed Jun 16, 2020
1 parent 1976fa6 commit 01e95af
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 65 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"html.format.wrapAttributes": "force-aligned",
"editor.minimap.enabled": false,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ Guid Typescript is a library that lets you generate guid code
### Installation

```
npm i guid-typescript --save
npm i typescript-guid --save
```

### Basic usage

```typescript
import { Guid } from "guid-typescript";
import { Guid } from "typescript-guid";

export class Example {
public id: Guid;
Expand All @@ -36,4 +36,3 @@ export class Example {
| toString ( ): string | Parse a guid instance to string format | OK | Ready |
| toJSON ( ): any | Parse to JSON format | OK | Ready |


55 changes: 28 additions & 27 deletions lib/guid.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
export class Guid {

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");

public static EMPTY = "00000000-0000-0000-0000-000000000000";

public static isGuid = (guid: any) => guid && (guid instanceof Guid || Guid.validator.test(guid.toString()));

public static create = (): Guid => new Guid([Guid.gen(2), Guid.gen(1), Guid.gen(1), Guid.gen(1), Guid.gen(3)].join("-"));

public static createEmpty = (): Guid => new Guid("emptyguid");

public static parse = (guid: string): Guid => new Guid(guid);
export const globals = {
guidDefaultValue: '00000000-0000-0000-0000-000000000000',
validationPattern: '^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$'
}

public static raw = (): string => [Guid.gen(2), Guid.gen(1), Guid.gen(1), Guid.gen(1), Guid.gen(3)].join("-");
export class Guid {

private static gen(count: number) {
let out: string = "";
for (let i: number = 0; i < count; i++) {
// tslint:disable-next-line:no-bitwise
out += (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
let out: string = '';
for (let i: number = 0; i < count; i++) out += (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
return out;
}

private value: string;

private constructor(guid: string) {
if (!guid) throw new TypeError("Invalid argument; `value` has no value.");
this.value = Guid.EMPTY;
protected constructor(guid: string) {
if (!guid) throw new TypeError('Invalid argument; `value` has no value.');
this.value = globals.guidDefaultValue;

if (guid && Guid.isGuid(guid)) this.value = guid;
}

static validator = new RegExp(globals.validationPattern, 'i');

static isGuid = (guid: any) => guid && (guid instanceof Guid || Guid.validator.test(guid.toString()));

static create = (): Guid => new Guid([Guid.gen(2), Guid.gen(1), Guid.gen(1), Guid.gen(1), Guid.gen(3)].join('-'));

static createEmpty = (): Guid => new Guid(globals.guidDefaultValue);

static parse = (guid: string): Guid => new Guid(guid);

static raw = (): string => [Guid.gen(2), Guid.gen(1), Guid.gen(1), Guid.gen(1), Guid.gen(3)].join('-');

static EMPTY = Guid.parse(globals.guidDefaultValue);

// Comparing string `value` against provided `guid` will auto-call
// toString on `guid` for comparison
public equals = (other: Guid): boolean => Guid.isGuid(other) && this.value === other.toString();
equals = (other: Guid): boolean => Guid.isGuid(other) && this.value === other.toString();

public isEmpty = (): boolean => this.value === Guid.EMPTY;
isEmpty = (): boolean => this.value === globals.guidDefaultValue;

public toString = (): string => this.value;
toString = (): string => this.value;

public toJSON(): any { return { value: this.value, }; }
toJSON(): any { return { value: this.value } }
}


10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "guid-typescript",
"version": "1.0.8",
"name": "typescript-guid",
"version": "1.0.0",
"description": "Guid generator to typescript",
"scripts": {
"verify": ".\\node_modules\\.bin\\tslint .\\lib\\**",
"test": "npm run verify && mocha -r ts-node/register tests/*.spec.ts",
"build": ".\\node_modules\\.bin\\tsc lib/guid -t es3 -m commonjs -d --outDir dist"
"build": ".\\node_modules\\.bin\\tsc -t es3 -m commonjs -d --outDir dist"
},
"author": "nicolas",
"license": "ISC",
Expand All @@ -18,7 +18,7 @@
"types": "./dist/guid.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/NicolasDeveloper/guid-typescript"
"url": "https://github.com/avmaisak/guid-typescript"
},
"devDependencies": {
"@types/chai": "^4.2.11",
Expand All @@ -34,4 +34,4 @@
"files": [
"/dist/"
]
}
}
45 changes: 21 additions & 24 deletions tests/guid.spec.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,61 @@
import { expect } from "chai";
import "mocha";
import { Guid } from "../lib/guid";
import { expect } from 'chai';
import 'mocha';
import { Guid } from '../lib/guid';

describe("Guid test", () => {
it("Should create a guid", () => {
const wrong = "wrongguid";
describe('Guid test', () => {

it('Should create a guid', () => {
const wrong = 'wrongguid';
expect(Guid.isGuid(wrong)).equal(false);

const right = Guid.create();
expect(Guid.isGuid(right)).equal(true);
});

it("Should raw a guid", () => {
const wrong = "wrongguid";
it('Should raw a guid', () => {
const wrong = 'wrongguid';
expect(Guid.isGuid(wrong)).equal(false);

const right = Guid.raw();
expect(Guid.isGuid(right)).equal(true);
});

it("Should compare another guid", () => {
it('Should compare another guid', () => {
const wrong = Guid.create();
expect(wrong.equals(Guid.create())).equal(false);

const right = Guid.create();
expect(right.equals(right)).equal(true);
});

it("Should compare another guid empty", () => {
const wrong = Guid.createEmpty();
it('Should compare another guid empty', () => {
const wrong = Guid.createEmpty();
expect(wrong.equals(Guid.create())).equal(false);

const right = Guid.createEmpty();
expect(right.equals(Guid.createEmpty())).equal(true);
});

it("Should verify if is guid", () => {
const wrong = "wrong guid";
it('Should verify if is guid', () => {
const wrong = 'wrong guid';
expect(Guid.isGuid(wrong)).equal(false);

const right = Guid.create();
expect(Guid.isGuid(right)).equal(true);
});

it("Should parse a guid", () => {
it('Should parse a guid', () => {
const wrong = Guid.raw();
expect(Guid.parse(wrong).equals(Guid.create())).equal(false);

const right = Guid.raw();
expect(Guid.parse(right).equals(Guid.parse(right))).equal(true);
});

it("Should be unique value", () => {
it('Should be unique value', () => {
const guids = [];
for (let index = 0; index < 3000; index++) {
guids.push(Guid.create());
}
for (let index = 0; index < 50000; index++) guids.push(Guid.create());
expect(guids.indexOf(guids[0]) < 0).equal(false);

expect(guids.indexOf(Guid.create()) < 0).equal(true);
});

Expand Down
13 changes: 11 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
"declarationMap": true,
"sourceMap": true,
"strict": true,
"esModuleInterop": true
}
"esModuleInterop": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
},
"include": [
"lib",
],
"exclude": [
"node_modules",
"**/__tests__/*"
]
}
19 changes: 15 additions & 4 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@
"extends": "tslint:recommended",
"rules": {
"max-line-length": {
"options": [400]
"options": [
400
]
},
"new-parens": true,
"no-arg": true,
"eofline": false,
"no-trailing-whitespace": false,
"no-bitwise": true,
"no-empty-interface": false,
"no-conditional-assignment": true,
"no-consecutive-blank-lines": false,
"variable-name": [true, "ban-keywords", "check-format", "allow-leading-underscore"],
"label-position": false,
"no-unused-expression": false,
"no-bitwise": false,
"variable-name": [
true,
"ban-keywords",
"check-format",
"allow-leading-underscore"
],
"no-console": {
"severity": "warning",
"options": [
Expand All @@ -27,7 +36,9 @@
},
"jsRules": {
"max-line-length": {
"options": [120]
"options": [
120
]
}
}
}

0 comments on commit 01e95af

Please sign in to comment.