nhb-toolbox 0.8.9 → 0.9.1

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.
@@ -8,4 +8,11 @@ import type { CapitalizeOptions } from './types';
8
8
  * @returns Capitalized string.
9
9
  */
10
10
  export declare const capitalizeString: (string: string, options?: CapitalizeOptions) => string;
11
+ /**
12
+ * Utility to truncate a string to a specified length.
13
+ * @param string The string to truncate.
14
+ * @param maxLength The maximum length of the truncated string.
15
+ * @returns Truncated string;
16
+ */
17
+ export declare const truncateString: (string: string, maxLength: number) => string;
11
18
  //# 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,YACL,iBAAiB,KAKxB,MA2CF,CAAC"}
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,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.capitalizeString = void 0;
3
+ 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.
@@ -9,19 +9,13 @@ exports.capitalizeString = void 0;
9
9
  * @param options Options to customize the capitalization.
10
10
  * @returns Capitalized string.
11
11
  */
12
- const capitalizeString = (string, options = {
13
- capitalizeAll: false,
14
- capitalizeEachFirst: false,
15
- lowerCaseRest: true,
16
- }) => {
17
- if (typeof string !== 'string' || !string.trim())
18
- return '';
19
- if (!string)
12
+ const capitalizeString = (string, options) => {
13
+ if (typeof string !== 'string' || !string)
20
14
  return '';
21
15
  const trimmedString = string.trim();
22
16
  if (!trimmedString)
23
17
  return '';
24
- const { capitalizeAll, capitalizeEachFirst, lowerCaseRest } = options;
18
+ const { capitalizeAll = false, capitalizeEachFirst = false, lowerCaseRest = true, } = options || {};
25
19
  if (capitalizeAll) {
26
20
  return trimmedString.toUpperCase();
27
21
  }
@@ -34,10 +28,9 @@ const capitalizeString = (string, options = {
34
28
  const matchArray = trimmedString.match(/^(\W*)(\w)(.*)$/);
35
29
  if (matchArray && matchArray.length === 4) {
36
30
  const [_, leadingSymbols, firstLetter, rest] = matchArray;
37
- return (leadingSymbols +
38
- firstLetter
39
- .toUpperCase()
40
- .concat(lowerCaseRest ? rest.toLowerCase() : rest));
31
+ return leadingSymbols
32
+ .concat(firstLetter.toUpperCase())
33
+ .concat(lowerCaseRest ? rest.toLowerCase() : rest);
41
34
  }
42
35
  return trimmedString
43
36
  .charAt(0)
@@ -47,3 +40,22 @@ const capitalizeString = (string, options = {
47
40
  : trimmedString.slice(1));
48
41
  };
49
42
  exports.capitalizeString = capitalizeString;
43
+ /**
44
+ * Utility to truncate a string to a specified length.
45
+ * @param string The string to truncate.
46
+ * @param maxLength The maximum length of the truncated string.
47
+ * @returns Truncated string;
48
+ */
49
+ const truncateString = (string, maxLength) => {
50
+ if (typeof string !== 'string' || !string)
51
+ return '';
52
+ const trimmedString = string.trim();
53
+ if (!trimmedString)
54
+ return '';
55
+ if (!trimmedString)
56
+ return '';
57
+ if (trimmedString.length <= maxLength)
58
+ return trimmedString;
59
+ return trimmedString.slice(0, maxLength).concat('...');
60
+ };
61
+ exports.truncateString = truncateString;
@@ -1,9 +1,9 @@
1
1
  export interface CapitalizeOptions {
2
- /** If true, capitalizes the first letter of each word (space separated). Defaults to `false` */
2
+ /** If true, capitalizes the first letter of each word (space separated). Defaults to `false`. */
3
3
  capitalizeEachFirst?: boolean;
4
- /** If true, ensures that the whole string is capitalized. Defaults to `false` */
4
+ /** If true, ensures that the whole string is capitalized. Defaults to `false`. */
5
5
  capitalizeAll?: boolean;
6
- /** If true, ensures that the rest of the string is lowercase. Defaults to `true` */
6
+ /** If true, ensures that the rest of the string is lowercase. Defaults to `true`. */
7
7
  lowerCaseRest?: boolean;
8
8
  }
9
9
  //# 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,gGAAgG;IAChG,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,iFAAiF;IACjF,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,oFAAoF;IACpF,aAAa,CAAC,EAAE,OAAO,CAAC;CACxB"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "0.8.9",
3
+ "version": "0.9.1",
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",