nhb-toolbox 0.8.8 → 0.9.0

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,9 +1,11 @@
1
+ import type { CapitalizeOptions } from './types';
1
2
  /**
2
3
  * Utility to convert the first letter of any string to uppercase and the rest lowercase (unless specified).
4
+ * Handles surrounding symbols like quotes or parentheses.
5
+ *
3
6
  * @param string String to be capitalized.
4
- * @param capEach If true, capitalizes the first letter of each word (space separated). Defaults to `false`.
5
- * @param lowerRest If true, ensures that the rest of the string is lowercase. Defaults to `true`.
7
+ * @param options Options to customize the capitalization.
6
8
  * @returns Capitalized string.
7
9
  */
8
- export declare const capitalizeString: (string: string, capEach?: boolean, lowerRest?: boolean) => string;
10
+ export declare const capitalizeString: (string: string, options?: CapitalizeOptions) => string;
9
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/string/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,WACpB,MAAM,YACL,OAAO,cACL,OAAO,KAChB,MAyBF,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,MA4CF,CAAC"}
@@ -3,34 +3,42 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  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
+ * Handles surrounding symbols like quotes or parentheses.
7
+ *
6
8
  * @param string String to be capitalized.
7
- * @param capEach If true, capitalizes the first letter of each word (space separated). Defaults to `false`.
8
- * @param lowerRest If true, ensures that the rest of the string is lowercase. Defaults to `true`.
9
+ * @param options Options to customize the capitalization.
9
10
  * @returns Capitalized string.
10
11
  */
11
- const capitalizeString = (string, capEach = false, lowerRest = true) => {
12
+ const capitalizeString = (string, options) => {
13
+ if (typeof string !== 'string' || !string.trim())
14
+ return '';
12
15
  if (!string)
13
16
  return '';
14
17
  const trimmedString = string.trim();
15
18
  if (!trimmedString)
16
19
  return '';
17
- if (capEach) {
20
+ const { capitalizeAll = false, capitalizeEachFirst = false, lowerCaseRest = true, } = options || {};
21
+ if (capitalizeAll) {
22
+ return trimmedString.toUpperCase();
23
+ }
24
+ if (capitalizeEachFirst) {
18
25
  return trimmedString
19
- .split(' ')
20
- .map((word) => (0, exports.capitalizeString)(word, false, lowerRest))
26
+ .split(/\s+/)
27
+ .map((word) => (0, exports.capitalizeString)(word, { lowerCaseRest }))
21
28
  .join(' ');
22
29
  }
23
- else {
24
- if (lowerRest) {
25
- return trimmedString
26
- .charAt(0)
27
- .toUpperCase()
28
- .concat(trimmedString.slice(1).toLowerCase());
29
- }
30
- return trimmedString
31
- .charAt(0)
32
- .toUpperCase()
33
- .concat(trimmedString.slice(1));
30
+ const matchArray = trimmedString.match(/^(\W*)(\w)(.*)$/);
31
+ if (matchArray && matchArray.length === 4) {
32
+ const [_, leadingSymbols, firstLetter, rest] = matchArray;
33
+ return leadingSymbols
34
+ .concat(firstLetter.toUpperCase())
35
+ .concat(lowerCaseRest ? rest.toLowerCase() : rest);
34
36
  }
37
+ return trimmedString
38
+ .charAt(0)
39
+ .toUpperCase()
40
+ .concat(lowerCaseRest
41
+ ? trimmedString.slice(1).toLowerCase()
42
+ : trimmedString.slice(1));
35
43
  };
36
44
  exports.capitalizeString = capitalizeString;
@@ -1,2 +1,9 @@
1
- export type TCapitalize = (string: string, capEach?: boolean, lowerRest?: boolean) => string;
1
+ export interface CapitalizeOptions {
2
+ /** If true, capitalizes the first letter of each word (space separated). Defaults to `false`. */
3
+ capitalizeEachFirst?: boolean;
4
+ /** If true, ensures that the whole string is capitalized. Defaults to `false`. */
5
+ capitalizeAll?: boolean;
6
+ /** If true, ensures that the rest of the string is lowercase. Defaults to `true`. */
7
+ lowerCaseRest?: boolean;
8
+ }
2
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,MAAM,WAAW,GAAG,CACzB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,OAAO,KACf,MAAM,CAAC"}
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.8",
3
+ "version": "0.9.0",
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",