nhb-toolbox 0.9.1 → 0.9.2

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.
@@ -1,4 +1,4 @@
1
- import type { CapitalizeOptions } from './types';
1
+ import type { CapitalizeOptions, RandomIDOptions } from './types';
2
2
  /**
3
3
  * Utility to convert the first letter of any string to uppercase and the rest lowercase (unless specified).
4
4
  * Handles surrounding symbols like quotes or parentheses.
@@ -10,9 +10,17 @@ import type { CapitalizeOptions } from './types';
10
10
  export declare const capitalizeString: (string: string, options?: CapitalizeOptions) => string;
11
11
  /**
12
12
  * Utility to truncate a string to a specified length.
13
+ *
13
14
  * @param string The string to truncate.
14
15
  * @param maxLength The maximum length of the truncated string.
15
16
  * @returns Truncated string;
16
17
  */
17
18
  export declare const truncateString: (string: string, maxLength: number) => string;
19
+ /**
20
+ * Generates a unique ID string composed of an optional `prefix`, `suffix`, a `timestamp`, `separator`, and a random alphanumeric string, separated by a customizable separator.
21
+ *
22
+ * @param options Configuration options for random ID generation.
23
+ * @returns The generated ID string composed of the prefix, timestamp, random alphanumeric string, and suffix, separated by the specified separator.
24
+ */
25
+ export declare const generateRandomID: (options?: RandomIDOptions) => string;
18
26
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/string/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,WACpB,MAAM,YACJ,iBAAiB,KACzB,MA0CF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,WAAY,MAAM,aAAa,MAAM,KAAG,MAYlE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/string/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAElE;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,WACpB,MAAM,YACJ,iBAAiB,KACzB,MA0CF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,WAAY,MAAM,aAAa,MAAM,KAAG,MAYlE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,aAAc,eAAe,KAAG,MAkC5D,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.truncateString = exports.capitalizeString = void 0;
3
+ exports.generateRandomID = exports.truncateString = exports.capitalizeString = void 0;
4
4
  /**
5
5
  * Utility to convert the first letter of any string to uppercase and the rest lowercase (unless specified).
6
6
  * Handles surrounding symbols like quotes or parentheses.
@@ -42,6 +42,7 @@ const capitalizeString = (string, options) => {
42
42
  exports.capitalizeString = capitalizeString;
43
43
  /**
44
44
  * Utility to truncate a string to a specified length.
45
+ *
45
46
  * @param string The string to truncate.
46
47
  * @param maxLength The maximum length of the truncated string.
47
48
  * @returns Truncated string;
@@ -59,3 +60,34 @@ const truncateString = (string, maxLength) => {
59
60
  return trimmedString.slice(0, maxLength).concat('...');
60
61
  };
61
62
  exports.truncateString = truncateString;
63
+ /**
64
+ * Generates a unique ID string composed of an optional `prefix`, `suffix`, a `timestamp`, `separator`, and a random alphanumeric string, separated by a customizable separator.
65
+ *
66
+ * @param options Configuration options for random ID generation.
67
+ * @returns The generated ID string composed of the prefix, timestamp, random alphanumeric string, and suffix, separated by the specified separator.
68
+ */
69
+ const generateRandomID = (options) => {
70
+ const { prefix = '', suffix = '', timeStamp = true, length = 13, separator = '.', caseOption = null, } = options || {};
71
+ // generate timestamp
72
+ const date = timeStamp ? Date.now() : '';
73
+ // Generate a random string of alphanumeric characters
74
+ const randomString = Array.from({ length }, () => Math.random().toString(36).slice(2, 3)).join('');
75
+ const ID = [
76
+ prefix && prefix.trim(),
77
+ date,
78
+ randomString,
79
+ suffix && suffix.trim(),
80
+ ]
81
+ .filter(Boolean)
82
+ .join(separator);
83
+ if (caseOption === 'upper') {
84
+ return ID.toUpperCase();
85
+ }
86
+ else if (caseOption === 'lower') {
87
+ return ID.toLowerCase();
88
+ }
89
+ else {
90
+ return ID;
91
+ }
92
+ };
93
+ exports.generateRandomID = generateRandomID;
@@ -1,3 +1,4 @@
1
+ /** Options for capitalizeString function. */
1
2
  export interface CapitalizeOptions {
2
3
  /** If true, capitalizes the first letter of each word (space separated). Defaults to `false`. */
3
4
  capitalizeEachFirst?: boolean;
@@ -6,4 +7,19 @@ export interface CapitalizeOptions {
6
7
  /** If true, ensures that the rest of the string is lowercase. Defaults to `true`. */
7
8
  lowerCaseRest?: boolean;
8
9
  }
10
+ /** Configuration options for ID generation. */
11
+ export interface RandomIDOptions {
12
+ /** A string to prepend to the ID. Default is an empty string. */
13
+ prefix?: string;
14
+ /** A string to append to the ID. Default is an empty string.*/
15
+ suffix?: string;
16
+ /** Whether to include the current timestamp in the ID. Default is `true`. */
17
+ timeStamp?: boolean;
18
+ /** The length of the random alphanumeric string. Default is `13`. */
19
+ length?: number;
20
+ /** The separator to use between parts of the ID. Default is a `period (.)`. */
21
+ separator?: string;
22
+ /** Specifies the case for the random alphanumeric string. */
23
+ caseOption?: 'upper' | 'lower' | null;
24
+ }
9
25
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/string/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,iBAAiB;IACjC,iGAAiG;IACjG,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,kFAAkF;IAClF,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,qFAAqF;IACrF,aAAa,CAAC,EAAE,OAAO,CAAC;CACxB"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/string/types.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,MAAM,WAAW,iBAAiB;IACjC,iGAAiG;IACjG,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,kFAAkF;IAClF,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,qFAAqF;IACrF,aAAa,CAAC,EAAE,OAAO,CAAC;CACxB;AAED,+CAA+C;AAC/C,MAAM,WAAW,eAAe;IAC/B,iEAAiE;IACjE,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,+DAA+D;IAC/D,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,6EAA6E;IAC7E,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB,qEAAqE;IACrE,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,+EAA+E;IAC/E,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,6DAA6D;IAC7D,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,GAAG,IAAI,CAAC;CACtC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "description": "A versatile collection of smart, efficient, and reusable utility functions for everyday development needs.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",