@adaas/a-utils 0.0.8 → 0.0.9
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.
- package/.nvmrc +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/src/helpers/A_ID.helper.d.ts +17 -0
- package/dist/src/helpers/A_ID.helper.js +26 -0
- package/dist/src/helpers/A_ID.helper.js.map +1 -0
- package/index.ts +1 -0
- package/package.json +1 -1
- package/src/helpers/A_ID.helper.ts +32 -0
- package/tests/default.test.ts +15 -2
package/.nvmrc
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
v22.20.0
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { A_CommonHelper } from './src/helpers/A_Common.helper';
|
|
2
|
+
export { A_IdentityHelper } from './src/helpers/A_ID.helper';
|
|
2
3
|
export { A_ScheduleHelper } from './src/helpers/A_Schedule.helper';
|
|
3
4
|
export { A_Error } from './src/global/A_Error.class';
|
|
4
5
|
export { A_ServerError } from './src/global/A_ServerError.class';
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// ====================== EXPORTS ======================
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.A_CONSTANTS__ERROR_CODES = exports.A_Polyfills = exports.A_ScheduleObject = exports.ASEID = exports.A_ServerError = exports.A_Error = exports.A_ScheduleHelper = exports.A_CommonHelper = void 0;
|
|
4
|
+
exports.A_CONSTANTS__ERROR_CODES = exports.A_Polyfills = exports.A_ScheduleObject = exports.ASEID = exports.A_ServerError = exports.A_Error = exports.A_ScheduleHelper = exports.A_IdentityHelper = exports.A_CommonHelper = void 0;
|
|
5
5
|
// --- Helpers ---
|
|
6
6
|
var A_Common_helper_1 = require("./src/helpers/A_Common.helper");
|
|
7
7
|
Object.defineProperty(exports, "A_CommonHelper", { enumerable: true, get: function () { return A_Common_helper_1.A_CommonHelper; } });
|
|
8
|
+
var A_ID_helper_1 = require("./src/helpers/A_ID.helper");
|
|
9
|
+
Object.defineProperty(exports, "A_IdentityHelper", { enumerable: true, get: function () { return A_ID_helper_1.A_IdentityHelper; } });
|
|
8
10
|
var A_Schedule_helper_1 = require("./src/helpers/A_Schedule.helper");
|
|
9
11
|
Object.defineProperty(exports, "A_ScheduleHelper", { enumerable: true, get: function () { return A_Schedule_helper_1.A_ScheduleHelper; } });
|
|
10
12
|
// --- Global ---
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";AAEA,wDAAwD;;;AAGxD,kBAAkB;AAClB,iEAA+D;AAAtD,iHAAA,cAAc,OAAA;AACvB,qEAAmE;AAA1D,qHAAA,gBAAgB,OAAA;AAGzB,iBAAiB;AACjB,4DAAqD;AAA5C,wGAAA,OAAO,OAAA;AAChB,wEAAiE;AAAxD,oHAAA,aAAa,OAAA;AAEtB,wDAAiD;AAAxC,oGAAA,KAAK,OAAA;AAEd,8EAAuE;AAA9D,0HAAA,gBAAgB,OAAA;AAIzB,wDAEiC;AAD7B,0GAAA,WAAW,OAAA;AAGf,oBAAoB;AACpB,qEAA4E;AAAnE,4HAAA,wBAAwB,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";AAEA,wDAAwD;;;AAGxD,kBAAkB;AAClB,iEAA+D;AAAtD,iHAAA,cAAc,OAAA;AACvB,yDAA6D;AAApD,+GAAA,gBAAgB,OAAA;AACzB,qEAAmE;AAA1D,qHAAA,gBAAgB,OAAA;AAGzB,iBAAiB;AACjB,4DAAqD;AAA5C,wGAAA,OAAO,OAAA;AAChB,wEAAiE;AAAxD,oHAAA,aAAa,OAAA;AAEtB,wDAAiD;AAAxC,oGAAA,KAAK,OAAA;AAEd,8EAAuE;AAA9D,0HAAA,gBAAgB,OAAA;AAIzB,wDAEiC;AAD7B,0GAAA,WAAW,OAAA;AAGf,oBAAoB;AACpB,qEAA4E;AAAnE,4HAAA,wBAAwB,OAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type A_ID_TYPES__TimeId_Parts = {
|
|
2
|
+
timestamp: Date;
|
|
3
|
+
random: string;
|
|
4
|
+
};
|
|
5
|
+
export declare class A_IdentityHelper {
|
|
6
|
+
/**
|
|
7
|
+
* Generates a short, time-based unique ID.
|
|
8
|
+
* Encodes current time (ms since epoch) and random bits in base36.
|
|
9
|
+
* Example: "mb4f1g-7f9a1c"
|
|
10
|
+
*/
|
|
11
|
+
static generateTimeId(parts?: A_ID_TYPES__TimeId_Parts): string;
|
|
12
|
+
/**
|
|
13
|
+
* Parses a short ID back into its parts.
|
|
14
|
+
* Returns an object with the original timestamp (as Date) and random string.
|
|
15
|
+
*/
|
|
16
|
+
static parseTimeId(id: string): A_ID_TYPES__TimeId_Parts;
|
|
17
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.A_IdentityHelper = void 0;
|
|
4
|
+
class A_IdentityHelper {
|
|
5
|
+
/**
|
|
6
|
+
* Generates a short, time-based unique ID.
|
|
7
|
+
* Encodes current time (ms since epoch) and random bits in base36.
|
|
8
|
+
* Example: "mb4f1g-7f9a1c"
|
|
9
|
+
*/
|
|
10
|
+
static generateTimeId(parts = { timestamp: new Date(), random: Math.random().toString(36).slice(2, 8) }) {
|
|
11
|
+
const time = parts.timestamp.getTime().toString(36); // base36-encoded timestamp
|
|
12
|
+
const random = parts.random; // use provided random string
|
|
13
|
+
return `${time}-${random}`;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Parses a short ID back into its parts.
|
|
17
|
+
* Returns an object with the original timestamp (as Date) and random string.
|
|
18
|
+
*/
|
|
19
|
+
static parseTimeId(id) {
|
|
20
|
+
const [timePart, randomPart] = id.split('-');
|
|
21
|
+
const timestamp = new Date(parseInt(timePart, 36));
|
|
22
|
+
return { timestamp, random: randomPart };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.A_IdentityHelper = A_IdentityHelper;
|
|
26
|
+
//# sourceMappingURL=A_ID.helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"A_ID.helper.js","sourceRoot":"","sources":["../../../src/helpers/A_ID.helper.ts"],"names":[],"mappings":";;;AAOA,MAAa,gBAAgB;IACzB;;;;KAIC;IACD,MAAM,CAAC,cAAc,CACjB,QAAkC,EAAE,SAAS,EAAE,IAAI,IAAI,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE;QAE3G,MAAM,IAAI,GAAG,KAAK,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,2BAA2B;QAChF,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,6BAA6B;QAC1D,OAAO,GAAG,IAAI,IAAI,MAAM,EAAE,CAAC;IAC/B,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,WAAW,CAAC,EAAU;QACzB,MAAM,CAAC,QAAQ,EAAE,UAAU,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC7C,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAC;QACnD,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;IAC7C,CAAC;CAEJ;AAxBD,4CAwBC"}
|
package/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adaas/a-utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"description": "A-Utils is a set of utilities that are used across the ADAAS ecosystem. This package is designed to be a collection of utilities that are used across the ADAAS ecosystem.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
export type A_ID_TYPES__TimeId_Parts = {
|
|
3
|
+
timestamp: Date;
|
|
4
|
+
random: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export class A_IdentityHelper {
|
|
9
|
+
/**
|
|
10
|
+
* Generates a short, time-based unique ID.
|
|
11
|
+
* Encodes current time (ms since epoch) and random bits in base36.
|
|
12
|
+
* Example: "mb4f1g-7f9a1c"
|
|
13
|
+
*/
|
|
14
|
+
static generateTimeId(
|
|
15
|
+
parts: A_ID_TYPES__TimeId_Parts = { timestamp: new Date(), random: Math.random().toString(36).slice(2, 8) }
|
|
16
|
+
): string {
|
|
17
|
+
const time = parts.timestamp.getTime().toString(36); // base36-encoded timestamp
|
|
18
|
+
const random = parts.random; // use provided random string
|
|
19
|
+
return `${time}-${random}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Parses a short ID back into its parts.
|
|
24
|
+
* Returns an object with the original timestamp (as Date) and random string.
|
|
25
|
+
*/
|
|
26
|
+
static parseTimeId(id: string): A_ID_TYPES__TimeId_Parts {
|
|
27
|
+
const [timePart, randomPart] = id.split('-');
|
|
28
|
+
const timestamp = new Date(parseInt(timePart, 36));
|
|
29
|
+
return { timestamp, random: randomPart };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
}
|
package/tests/default.test.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { A_CommonHelper } from '@adaas/a-utils/helpers/A_Common.helper';
|
|
2
|
+
import { A_IdentityHelper } from '@adaas/a-utils/helpers/A_ID.helper';
|
|
2
3
|
import { A_ScheduleHelper } from '@adaas/a-utils/helpers/A_Schedule.helper';
|
|
3
4
|
import { A_TYPES__DeepPartial } from '@adaas/a-utils/types/A_Common.types';
|
|
4
5
|
import { config } from 'dotenv';
|
|
@@ -147,8 +148,6 @@ describe('CommonHelper Tests', () => {
|
|
|
147
148
|
|
|
148
149
|
const merged = A_CommonHelper.deepCloneAndMerge(t2, t);
|
|
149
150
|
|
|
150
|
-
console.log('merged: ', merged)
|
|
151
|
-
|
|
152
151
|
expect(merged.a).toBe('a');
|
|
153
152
|
expect(merged.b).toBe('bb');
|
|
154
153
|
expect(merged.c.d).toBe('ddd');
|
|
@@ -157,4 +156,18 @@ describe('CommonHelper Tests', () => {
|
|
|
157
156
|
expect((merged as any).some.d).toBe('dd');
|
|
158
157
|
expect(merged.f('names')).toBe('names');
|
|
159
158
|
});
|
|
159
|
+
it('should generate and then parse Unique time based IDs', async () => {
|
|
160
|
+
const id = A_IdentityHelper.generateTimeId();
|
|
161
|
+
const parts = A_IdentityHelper.parseTimeId(id);
|
|
162
|
+
|
|
163
|
+
expect(id).toBeDefined();
|
|
164
|
+
expect(parts.timestamp).toBeInstanceOf(Date);
|
|
165
|
+
expect(parts.random).toHaveLength(6);
|
|
166
|
+
|
|
167
|
+
// Check that the timestamp is recent (within the last minute)
|
|
168
|
+
const now = Date.now();
|
|
169
|
+
const timestamp = parts.timestamp.getTime();
|
|
170
|
+
expect(timestamp).toBeLessThanOrEqual(now);
|
|
171
|
+
expect(timestamp).toBeGreaterThan(now - 60000); // within the last minute
|
|
172
|
+
});
|
|
160
173
|
});
|