@pocket-architect/core 0.1.12 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,10 @@ export declare class EntityId<T> {
2
2
  protected _recordId: T | null;
3
3
  protected _uuid: string;
4
4
  constructor(recordId?: T, uuid?: string);
5
+ private createUUID;
6
+ get hashOptions(): [string, number];
7
+ toHash(): string;
8
+ fromHash(hash: string): EntityId<T>;
5
9
  toPrimitive(): T;
6
10
  toString(): string;
7
11
  equals(id: EntityId<T>): boolean;
package/build/EntityId.js CHANGED
@@ -4,6 +4,7 @@ exports.EntityId = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var uuid_by_string_1 = tslib_1.__importDefault(require("uuid-by-string"));
6
6
  var cuid2_1 = require("@paralleldrive/cuid2");
7
+ var hashids_1 = tslib_1.__importDefault(require("hashids"));
7
8
  var EntityId = /** @class */ (function () {
8
9
  function EntityId(recordId, uuid) {
9
10
  if (recordId === void 0) { recordId = null; }
@@ -12,9 +13,36 @@ var EntityId = /** @class */ (function () {
12
13
  this._uuid = null;
13
14
  this._recordId = recordId ? recordId : null;
14
15
  if (!uuid) {
15
- this._uuid = (0, uuid_by_string_1.default)(recordId ? recordId.toString() : (0, cuid2_1.createId)(), (0, uuid_by_string_1.default)(this.constructor.name));
16
+ this.createUUID();
16
17
  }
17
18
  }
19
+ EntityId.prototype.createUUID = function () {
20
+ this._uuid = (0, uuid_by_string_1.default)(this._recordId ? this._recordId.toString() : (0, cuid2_1.createId)(), (0, uuid_by_string_1.default)(this.constructor.name));
21
+ };
22
+ Object.defineProperty(EntityId.prototype, "hashOptions", {
23
+ get: function () {
24
+ return [this.constructor.name, 5];
25
+ },
26
+ enumerable: false,
27
+ configurable: true
28
+ });
29
+ EntityId.prototype.toHash = function () {
30
+ if (!this._recordId) {
31
+ throw new Error('Cannot hash an empty recordId');
32
+ }
33
+ var hashids = new (hashids_1.default.bind.apply(hashids_1.default, tslib_1.__spreadArray([void 0], this.hashOptions, false)))();
34
+ return hashids.encode(this.toPrimitive().toString());
35
+ };
36
+ EntityId.prototype.fromHash = function (hash) {
37
+ if (!this._recordId) {
38
+ throw new Error('Cannot hash an empty recordId');
39
+ }
40
+ var hashids = new (hashids_1.default.bind.apply(hashids_1.default, tslib_1.__spreadArray([void 0], this.hashOptions, false)))();
41
+ var num = hashids.decode(hash)[0];
42
+ this._recordId = (typeof this._recordId === 'string' ? num.toString() : num);
43
+ this.createUUID();
44
+ return this;
45
+ };
18
46
  EntityId.prototype.toPrimitive = function () {
19
47
  return this._recordId;
20
48
  };
@@ -1 +1 @@
1
- {"version":3,"file":"EntityId.js","sourceRoot":"","sources":["../src/EntityId.ts"],"names":[],"mappings":";;;;AAAA,0EAAuC;AACvC,8CAA+C;AAE/C;IAIE,kBAAY,QAAkB,EAAE,IAAmB;QAAvC,yBAAA,EAAA,eAAkB;QAAE,qBAAA,EAAA,WAAmB;QAHzC,cAAS,GAAW,IAAI,CAAC;QACzB,UAAK,GAAW,IAAI,CAAC;QAG7B,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC;QAC5C,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,IAAI,CAAC,KAAK,GAAG,IAAA,wBAAU,EAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAA,gBAAQ,GAAE,EAAE,IAAA,wBAAU,EAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;QAC1G,CAAC;IACH,CAAC;IAED,8BAAW,GAAX;QACE,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,2BAAQ,GAAR;QACE,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,yBAAM,GAAN,UAAO,EAAe;QACpB,OAAO,IAAI,CAAC,KAAK,KAAK,EAAE,CAAC,KAAK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AED,2BAAQ,GAAR,UAAS,IAAY;QACnB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;QACnD,CAAC;QACD,IAAM,OAAO,QAAO,iBAAO,YAAP,iBAAO,kCAAI,IAAI,CAAC,WAAW,YAAC,CAAC;QACjD,IAAM,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QACpC,IAAI,CAAC,SAAS,GAAM,CAAC,OAAO,IAAI,CAAC,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAChF,IAAI,CAAC,UAAU,EAAE,CAAC;QAClB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,8BAAW,GAAX;QACE,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,2BAAQ,GAAR;QACE,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,yBAAM,GAAN,UAAO,EAAe;QACpB,OAAO,IAAI,CAAC,KAAK,KAAK,EAAE,CAAC,KAAK,CAAC;IACjC,CAAC;IACH,eAAC;AAAD,CAAC,AAjDD,IAiDC;AAjDY,4BAAQ"}
@@ -9,6 +9,13 @@ var TestId = /** @class */ (function (_super) {
9
9
  }
10
10
  return TestId;
11
11
  }(EntityId_1.EntityId));
12
+ var Test2Id = /** @class */ (function (_super) {
13
+ tslib_1.__extends(Test2Id, _super);
14
+ function Test2Id() {
15
+ return _super !== null && _super.apply(this, arguments) || this;
16
+ }
17
+ return Test2Id;
18
+ }(EntityId_1.EntityId));
12
19
  describe('ValueObject', function () {
13
20
  test('base', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
14
21
  var id, id2, id3;
@@ -21,5 +28,22 @@ describe('ValueObject', function () {
21
28
  return [2 /*return*/];
22
29
  });
23
30
  }); });
31
+ test('toHash', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
32
+ var id, id2, id3;
33
+ return tslib_1.__generator(this, function (_a) {
34
+ id = new Test2Id('123');
35
+ expect(id.toHash()).toEqual('9KdMR');
36
+ expect(id.fromHash('VLoxm').toPrimitive()).toEqual('1');
37
+ expect(id.toPrimitive()).toEqual('1');
38
+ expect(id.toString()).toEqual('090bb5d1-267b-5967-84c8-95bc1bda380f');
39
+ id2 = new TestId(123);
40
+ expect(id2.toHash()).toEqual('BJejP');
41
+ expect(id2.fromHash('BJejP').toPrimitive()).toEqual(123);
42
+ id3 = new Test2Id();
43
+ expect(function () { return id3.toHash(); }).toThrow('Cannot hash an empty recordId');
44
+ expect(function () { return id3.fromHash('123'); }).toThrow('Cannot hash an empty recordId');
45
+ return [2 /*return*/];
46
+ });
47
+ }); });
24
48
  });
25
49
  //# sourceMappingURL=EntityId.spec.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"EntityId.spec.js","sourceRoot":"","sources":["../src/EntityId.spec.ts"],"names":[],"mappings":";;;AAAA,uCAAsC;AAEtC;IAAqB,kCAAgB;IAArC;;IACA,CAAC;IAAD,aAAC;AAAD,CAAC,AADD,CAAqB,mBAAQ,GAC5B;AAED,QAAQ,CAAC,aAAa,EAAE;IACtB,IAAI,CAAC,MAAM,EAAE;;;YACL,EAAE,GAAG,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC;YACnB,GAAG,GAAG,IAAI,mBAAQ,CAAC,GAAG,CAAC,CAAC;YACxB,GAAG,GAAG,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC;YAC1B,MAAM,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YACxB,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;;;SAC7B,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
1
+ {"version":3,"file":"EntityId.spec.js","sourceRoot":"","sources":["../src/EntityId.spec.ts"],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CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YAEnD,GAAG,GAAG,IAAI,OAAO,EAAE,CAAC;YAC1B,MAAM,CAAC,cAAM,OAAA,GAAG,CAAC,MAAM,EAAE,EAAZ,CAAY,CAAC,CAAC,OAAO,CAAC,+BAA+B,CAAC,CAAC;YACpE,MAAM,CAAC,cAAM,OAAA,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAnB,CAAmB,CAAC,CAAC,OAAO,CAAC,+BAA+B,CAAC,CAAC;;;SAC5E,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pocket-architect/core",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -17,6 +17,7 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "@paralleldrive/cuid2": "^2.2.2",
20
+ "hashids": "^2.3.0",
20
21
  "shallow-equal-object": "^1.1.1",
21
22
  "uuid-by-string": "^4.0.0"
22
23
  },
@@ -3,6 +3,9 @@ import { EntityId } from './EntityId';
3
3
  class TestId extends EntityId<number> {
4
4
  }
5
5
 
6
+ class Test2Id extends EntityId<string> {
7
+ }
8
+
6
9
  describe('ValueObject', () => {
7
10
  test('base', async () => {
8
11
  const id = new TestId(1);
@@ -11,4 +14,20 @@ describe('ValueObject', () => {
11
14
  expect(id).toEqual(id3);
12
15
  expect(id).not.toEqual(id2);
13
16
  });
17
+
18
+ test('toHash', async () => {
19
+ const id = new Test2Id('123');
20
+ expect(id.toHash()).toEqual('9KdMR');
21
+ expect(id.fromHash('VLoxm').toPrimitive()).toEqual('1');
22
+ expect(id.toPrimitive()).toEqual('1');
23
+ expect(id.toString()).toEqual('090bb5d1-267b-5967-84c8-95bc1bda380f');
24
+
25
+ const id2 = new TestId(123);
26
+ expect(id2.toHash()).toEqual('BJejP');
27
+ expect(id2.fromHash('BJejP').toPrimitive()).toEqual(123);
28
+
29
+ const id3 = new Test2Id();
30
+ expect(() => id3.toHash()).toThrow('Cannot hash an empty recordId');
31
+ expect(() => id3.fromHash('123')).toThrow('Cannot hash an empty recordId');
32
+ });
14
33
  });
package/src/EntityId.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import createUUID from 'uuid-by-string'
2
2
  import { createId } from '@paralleldrive/cuid2'
3
+ import Hashids from 'hashids';
3
4
 
4
5
  export class EntityId<T> {
5
6
  protected _recordId: T|null = null;
@@ -8,10 +9,37 @@ export class EntityId<T> {
8
9
  constructor(recordId: T = null, uuid: string = null) {
9
10
  this._recordId = recordId ? recordId : null;
10
11
  if (!uuid) {
11
- this._uuid = createUUID(recordId ? recordId.toString() : createId(), createUUID(this.constructor.name));
12
+ this.createUUID();
12
13
  }
13
14
  }
14
15
 
16
+ private createUUID():void {
17
+ this._uuid = createUUID(this._recordId ? this._recordId.toString() : createId(), createUUID(this.constructor.name));
18
+ }
19
+
20
+ get hashOptions() : [string, number] {
21
+ return [this.constructor.name, 5];
22
+ }
23
+
24
+ toHash(): string {
25
+ if (!this._recordId) {
26
+ throw new Error('Cannot hash an empty recordId');
27
+ }
28
+ const hashids = new Hashids(...this.hashOptions);
29
+ return hashids.encode(this.toPrimitive().toString());
30
+ }
31
+
32
+ fromHash(hash: string) : EntityId<T> {
33
+ if (!this._recordId) {
34
+ throw new Error('Cannot hash an empty recordId');
35
+ }
36
+ const hashids = new Hashids(...this.hashOptions);
37
+ const num = hashids.decode(hash)[0];
38
+ this._recordId = <T>(typeof this._recordId === 'string' ? num.toString() : num);
39
+ this.createUUID();
40
+ return this;
41
+ }
42
+
15
43
  toPrimitive(): T {
16
44
  return this._recordId;
17
45
  }