@stryke/string-format 0.5.2 → 0.6.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.
@@ -4,8 +4,12 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.camelCase = camelCase;
7
+ exports.isCamelCase = isCamelCase;
7
8
  var _lowerCaseFirst = require("./lower-case-first.cjs");
8
9
  var _pascalCase = require("./pascal-case.cjs");
10
+ function isCamelCase(e) {
11
+ return e ? /^[a-z][a-zA-Z0-9]*$/.test(e) : !1;
12
+ }
9
13
  function camelCase(e) {
10
- return e && (0, _lowerCaseFirst.lowerCaseFirst)((0, _pascalCase.pascalCase)(e));
14
+ return isCamelCase(e) || e === void 0 ? e : (0, _lowerCaseFirst.lowerCaseFirst)((0, _pascalCase.pascalCase)(e));
11
15
  }
@@ -1,8 +1,18 @@
1
+ /**
2
+ * Check if the input string is in camel case.
3
+ *
4
+ * @remarks
5
+ * Camel case is defined as a lowercase first letter followed by any number of uppercase letters and digits - "thisIsAnExample".
6
+ *
7
+ * @param input - The input string to check.
8
+ * @returns True if the input is in camel case, false otherwise.
9
+ */
10
+ export declare function isCamelCase(input: string | undefined): boolean;
1
11
  /**
2
12
  * Convert the input string to camel case.
3
13
  *
4
14
  * @remarks
5
- * "thisIsAnExample"
15
+ * Camel case is defined as a lowercase first letter followed by any number of uppercase letters and digits - "thisIsAnExample".
6
16
  *
7
17
  * @param input - The input string.
8
18
  * @returns The camel-cased string.
@@ -1 +1 @@
1
- import{lowerCaseFirst as r}from"./lower-case-first";import{pascalCase as o}from"./pascal-case";export function camelCase(e){return e&&r(o(e))}
1
+ import{lowerCaseFirst as r}from"./lower-case-first";import{pascalCase as a}from"./pascal-case";export function isCamelCase(e){return e?/^[a-z][a-zA-Z0-9]*$/.test(e):!1}export function camelCase(e){return isCamelCase(e)||e===void 0?e:r(a(e))}
@@ -4,7 +4,12 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.constantCase = constantCase;
7
+ exports.isConstantCase = isConstantCase;
8
+ var _getWords = require("./get-words.cjs");
7
9
  var _snakeCase = require("./snake-case.cjs");
10
+ function isConstantCase(e) {
11
+ return e ? /^[A-Z0-9_]+$/.test(e) : !1;
12
+ }
8
13
  function constantCase(e) {
9
- return (0, _snakeCase.snakeCase)(e)?.toUpperCase();
14
+ return isConstantCase(e) || e === void 0 ? e : (0, _snakeCase.isSnakeCase)(e) ? e.toUpperCase() : (0, _getWords.getWords)(e).join("_").toUpperCase();
10
15
  }
@@ -1,8 +1,18 @@
1
+ /**
2
+ * Check if the input string is in constant case.
3
+ *
4
+ * @remarks
5
+ * Constant case is defined as all uppercase letters with underscores separating words - "THIS_IS_AN_EXAMPLE".
6
+ *
7
+ * @param input - The input string to check.
8
+ * @returns True if the input is in constant case, false otherwise.
9
+ */
10
+ export declare function isConstantCase(input: string | undefined): boolean;
1
11
  /**
2
12
  * Convert the input string to constant case.
3
13
  *
4
14
  * @remarks
5
- * "THIS_IS_AN_EXAMPLE"
15
+ * Constant case is defined as all uppercase letters with underscores separating words - "THIS_IS_AN_EXAMPLE".
6
16
  *
7
17
  * @param input - The input string.
8
18
  * @returns The constant-cased string.
@@ -1 +1 @@
1
- import{snakeCase as n}from"./snake-case";export function constantCase(e){return n(e)?.toUpperCase()}
1
+ import{getWords as s}from"./get-words";import{isSnakeCase as n}from"./snake-case";export function isConstantCase(e){return e?/^[A-Z0-9_]+$/.test(e):!1}export function constantCase(e){return isConstantCase(e)||e===void 0?e:n(e)?e.toUpperCase():s(e).join("_").toUpperCase()}
@@ -3,9 +3,14 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.isKebabCase = isKebabCase;
6
7
  exports.kebabCase = kebabCase;
7
8
  var _getWords = require("./get-words.cjs");
9
+ function isKebabCase(e) {
10
+ return e ? /^[a-z]+(?:-[a-z0-9]+)*$/.test(e) : !1;
11
+ }
8
12
  function kebabCase(e) {
13
+ if (isKebabCase(e) || e === void 0) return e;
9
14
  const r = e ? (0, _getWords.getWords)(e) : [];
10
15
  return r.length === 0 ? "" : r.join("-").toLowerCase();
11
16
  }
@@ -1,8 +1,18 @@
1
+ /**
2
+ * Check if the input string is in kebab case.
3
+ *
4
+ * @remarks
5
+ * Kebab case is defined as all lowercase letters with hyphens separating words - "this-is-an-example".
6
+ *
7
+ * @param input - The input string to check.
8
+ * @returns True if the input is in kebab case, false otherwise.
9
+ */
10
+ export declare function isKebabCase(input: string | undefined): boolean;
1
11
  /**
2
12
  * Convert the input string to kebab case.
3
13
  *
4
14
  * @remarks
5
- * "this-is-an-example"
15
+ * Kebab case is defined as all lowercase letters with hyphens separating words - "this-is-an-example".
6
16
  *
7
17
  * @param input - The input string.
8
18
  * @returns The kebab-cased string.
@@ -1 +1 @@
1
- import{getWords as t}from"./get-words";export function kebabCase(e){const r=e?t(e):[];return r.length===0?"":r.join("-").toLowerCase()}
1
+ import{getWords as n}from"./get-words";export function isKebabCase(e){return e?/^[a-z]+(?:-[a-z0-9]+)*$/.test(e):!1}export function kebabCase(e){if(isKebabCase(e)||e===void 0)return e;const r=e?n(e):[];return r.length===0?"":r.join("-").toLowerCase()}
@@ -3,7 +3,12 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.isPascalCase = isPascalCase;
6
7
  exports.pascalCase = pascalCase;
7
- function pascalCase(t) {
8
- return t && t.split(" ").map(e => e.split("-")).flat().map(e => e.length > 0 ? e.trim().charAt(0).toUpperCase() + e.trim().slice(1) : "").join("");
8
+ var _getWords = require("./get-words.cjs");
9
+ function isPascalCase(e) {
10
+ return e ? /^[A-Z][a-zA-Z0-9]*$/.test(e) : !1;
11
+ }
12
+ function pascalCase(e) {
13
+ return isPascalCase(e) || e === void 0 ? e : (0, _getWords.getWords)(e).map(r => r.length > 0 ? r.trim().charAt(0).toUpperCase() + r.trim().slice(1) : "").join("");
9
14
  }
@@ -1,8 +1,18 @@
1
+ /**
2
+ * Check if the input string is in pascal case.
3
+ *
4
+ * @remarks
5
+ * Pascal case is defined as an uppercase first letter followed by any number of lowercase letters and digits - "ThisIsAnExample".
6
+ *
7
+ * @param input - The input string to check.
8
+ * @returns True if the input is in pascal case, false otherwise.
9
+ */
10
+ export declare function isPascalCase(input: string | undefined): boolean;
1
11
  /**
2
12
  * Convert the input string to pascal case.
3
13
  *
4
14
  * @remarks
5
- * "ThisIsAnExample"
15
+ * Pascal case is defined as an uppercase first letter followed by any number of lowercase letters and digits - "ThisIsAnExample".
6
16
  *
7
17
  * @param input - The input string.
8
18
  * @returns The pascal-cased string.
@@ -1 +1 @@
1
- export function pascalCase(t){return t&&t.split(" ").map(e=>e.split("-")).flat().map(e=>e.length>0?e.trim().charAt(0).toUpperCase()+e.trim().slice(1):"").join("")}
1
+ import{getWords as n}from"./get-words";export function isPascalCase(e){return e?/^[A-Z][a-zA-Z0-9]*$/.test(e):!1}export function pascalCase(e){return isPascalCase(e)||e===void 0?e:n(e).map(r=>r.length>0?r.trim().charAt(0).toUpperCase()+r.trim().slice(1):"").join("")}
@@ -3,14 +3,20 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.isSnakeCase = isSnakeCase;
6
7
  exports.snakeCase = snakeCase;
7
8
  var _types = require("@stryke/types");
9
+ var _constantCase = require("./constant-case.cjs");
8
10
  var _upperCaseFirst = require("./upper-case-first.cjs");
9
- function snakeCase(t, n) {
10
- if (!t) return t;
11
- const r = t?.replace(/[A-Z]+/g, e => (0, _upperCaseFirst.upperCaseFirst)(e) ?? _types.EMPTY_STRING).split(/(?=[A-Z])|[\s._-]/).map(e => e.toLowerCase()) ?? [];
12
- if (r.length === 0) return "";
13
- if (r.length === 1) return r[0];
14
- const s = r.reduce((e, i) => `${e}_${i.toLowerCase()}`);
15
- return n?.splitOnNumber === !1 ? s : s.replace(/[A-Z]\d/i, e => `${e[0]}_${e[1]}`);
11
+ function isSnakeCase(e) {
12
+ return e ? /^[a-z]+(?:_[a-z0-9]+)*$/.test(e) : !1;
13
+ }
14
+ function snakeCase(e, n) {
15
+ if (isSnakeCase(e) || e === void 0) return e;
16
+ if ((0, _constantCase.isConstantCase)(e)) return e.toLowerCase();
17
+ const s = e?.replace(/[A-Z]+/g, r => (0, _upperCaseFirst.upperCaseFirst)(r) ?? _types.EMPTY_STRING).split(/(?=[A-Z])|[\s._-]/).map(r => r.toLowerCase()) ?? [];
18
+ if (s.length === 0) return "";
19
+ if (s.length === 1) return s[0];
20
+ const t = s.reduce((r, a) => `${r}_${a.toLowerCase()}`);
21
+ return n?.splitOnNumber === !1 ? t : t.replace(/[A-Z]\d/i, r => `${r[0]}_${r[1]}`);
16
22
  }
@@ -1,11 +1,21 @@
1
1
  export interface SnakeCaseOptions {
2
2
  splitOnNumber: boolean;
3
3
  }
4
+ /**
5
+ * Check if the input string is in snake case.
6
+ *
7
+ * @remarks
8
+ * Snake case is defined as all lowercase letters with underscores separating words - "this_is_an_example"
9
+ *
10
+ * @param input - The input string to check.
11
+ * @returns True if the input is in snake case, false otherwise.
12
+ */
13
+ export declare function isSnakeCase(input: string | undefined): boolean;
4
14
  /**
5
15
  * Convert the input string to snake case.
6
16
  *
7
17
  * @remarks
8
- * "this_is_an_example"
18
+ * Snake case is defined as all lowercase letters with underscores separating words - "this_is_an_example"
9
19
  *
10
20
  * @param input - The input string.
11
21
  * @param options - Options to control the behavior of the function.
@@ -1 +1 @@
1
- import{EMPTY_STRING as a}from"@stryke/types";import{upperCaseFirst as o}from"./upper-case-first";export function snakeCase(t,n){if(!t)return t;const r=t?.replace(/[A-Z]+/g,e=>o(e)??a).split(/(?=[A-Z])|[\s._-]/).map(e=>e.toLowerCase())??[];if(r.length===0)return"";if(r.length===1)return r[0];const s=r.reduce((e,i)=>`${e}_${i.toLowerCase()}`);return n?.splitOnNumber===!1?s:s.replace(/[A-Z]\d/i,e=>`${e[0]}_${e[1]}`)}
1
+ import{EMPTY_STRING as o}from"@stryke/types";import{isConstantCase as i}from"./constant-case";import{upperCaseFirst as f}from"./upper-case-first";export function isSnakeCase(e){return e?/^[a-z]+(?:_[a-z0-9]+)*$/.test(e):!1}export function snakeCase(e,n){if(isSnakeCase(e)||e===void 0)return e;if(i(e))return e.toLowerCase();const s=e?.replace(/[A-Z]+/g,r=>f(r)??o).split(/(?=[A-Z])|[\s._-]/).map(r=>r.toLowerCase())??[];if(s.length===0)return"";if(s.length===1)return s[0];const t=s.reduce((r,a)=>`${r}_${a.toLowerCase()}`);return n?.splitOnNumber===!1?t:t.replace(/[A-Z]\d/i,r=>`${r[0]}_${r[1]}`)}
@@ -3,12 +3,16 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.isStartCase = isStartCase;
6
7
  exports.startCase = startCase;
7
8
  var _getWords = require("./get-words.cjs");
8
- function startCase(t) {
9
- if (!t) return t;
10
- const o = (0, _getWords.getWords)(t.trim());
11
- let r = "";
12
- for (const e of o) e && e[0] && (r && (r += " "), r += e === e.toUpperCase() ? e : e[0].toUpperCase() + e.slice(1).toLowerCase());
13
- return r;
9
+ function isStartCase(e) {
10
+ return e ? /^[A-Z][a-z]*(?: [A-Z][a-z]*)*$/.test(e) : !1;
11
+ }
12
+ function startCase(e) {
13
+ if (isStartCase(e) || e === void 0) return e;
14
+ const s = (0, _getWords.getWords)(e.trim());
15
+ let t = "";
16
+ for (const r of s) r && r[0] && (t && (t += " "), t += r === r.toUpperCase() ? r : r[0].toUpperCase() + r.slice(1).toLowerCase());
17
+ return t;
14
18
  }
@@ -1,10 +1,18 @@
1
1
  /**
2
- * Converts the first character of each word in a string to uppercase and the remaining characters to lowercase.
2
+ * Check if the input string is in snake case.
3
3
  *
4
4
  * @remarks
5
- * "This Is An Example"
5
+ * Start case is the naming convention in which each word is written with an initial capital letter - "This Is An Example".
6
+ *
7
+ * @param input - The input string to check.
8
+ * @returns True if the input is in start case, false otherwise.
9
+ */
10
+ export declare function isStartCase(input: string | undefined): boolean;
11
+ /**
12
+ * Converts the first character of each word in a string to uppercase and the remaining characters to lowercase.
6
13
  *
7
- * Start case is the naming convention in which each word is written with an initial capital letter.
14
+ * @remarks
15
+ * Start case is the naming convention in which each word is written with an initial capital letter - "This Is An Example".
8
16
  *
9
17
  * @example
10
18
  * ```ts
@@ -1 +1 @@
1
- import{getWords as s}from"./get-words";export function startCase(t){if(!t)return t;const o=s(t.trim());let r="";for(const e of o)e&&e[0]&&(r&&(r+=" "),r+=e===e.toUpperCase()?e:e[0].toUpperCase()+e.slice(1).toLowerCase());return r}
1
+ import{getWords as o}from"./get-words";export function isStartCase(e){return e?/^[A-Z][a-z]*(?: [A-Z][a-z]*)*$/.test(e):!1}export function startCase(e){if(isStartCase(e)||e===void 0)return e;const s=o(e.trim());let t="";for(const r of s)r&&r[0]&&(t&&(t+=" "),t+=r===r.toUpperCase()?r:r[0].toUpperCase()+r.slice(1).toLowerCase());return t}
@@ -4,17 +4,21 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.LOWER_CASE_WHEN_NOT_FIRST = exports.FORMAT_MAPPING = void 0;
7
+ exports.isTitleCase = isTitleCase;
7
8
  exports.titleCase = titleCase;
8
9
  var _acronyms = require("./acronyms.cjs");
9
10
  var _upperCaseFirst = require("./upper-case-first.cjs");
10
- const FORMAT_MAPPING = exports.FORMAT_MAPPING = _acronyms.ACRONYMS.reduce((s, e) => (s[e.toLowerCase()] = e, s), {
11
+ const FORMAT_MAPPING = exports.FORMAT_MAPPING = _acronyms.ACRONYMS.reduce((e, t) => (e[t.toLowerCase()] = t, e), {
11
12
  cspell: "CSpell",
12
13
  eslint: "ESLint",
13
14
  nx: "Nx"
14
15
  }),
15
16
  LOWER_CASE_WHEN_NOT_FIRST = exports.LOWER_CASE_WHEN_NOT_FIRST = ["a", "an", "the", "is", "are", "of", "and", "to", "in", "for", "on", "with", "as", "at", "by"];
16
- function titleCase(s, e = {}) {
17
- if (!s) return s;
18
- const n = r => r.toLowerCase().split(/[\s\-_]+/).filter(Boolean).map((t, i) => !e.skipLowerCaseWhenNotFirst && LOWER_CASE_WHEN_NOT_FIRST.includes(t.toLowerCase()) && i > 0 ? t.toLowerCase() : !e.skipFormatMapping && Object.keys(FORMAT_MAPPING).includes(t.toLowerCase()) ? FORMAT_MAPPING[t.toLowerCase()] : e.mapping && Object.keys(e.mapping).includes(t.toLowerCase()) ? e.mapping[t.toLowerCase()] : `${(0, _upperCaseFirst.upperCaseFirst)(t.toLowerCase())}`).join(" ");
19
- return s.split(/\s+-\s+/).map(r => n(r)).join(" - ");
17
+ function isTitleCase(e) {
18
+ return e ? e.split(/[\s\-_]+/).filter(Boolean).every((s, n) => Object.values(FORMAT_MAPPING).includes(s) || n > 0 && LOWER_CASE_WHEN_NOT_FIRST.includes(s.toLowerCase()) && s === s.toLowerCase() ? !0 : /^[A-Z][a-z0-9]*$/.test(s)) : !1;
19
+ }
20
+ function titleCase(e, t = {}) {
21
+ if (isTitleCase(e) || e === void 0) return e;
22
+ const s = n => n.toLowerCase().split(/[\s\-_]+/).filter(Boolean).map((r, i) => !t.skipLowerCaseWhenNotFirst && LOWER_CASE_WHEN_NOT_FIRST.includes(r.toLowerCase()) && i > 0 ? r.toLowerCase() : !t.skipFormatMapping && Object.keys(FORMAT_MAPPING).includes(r.toLowerCase()) ? FORMAT_MAPPING[r.toLowerCase()] : t.mapping && Object.keys(t.mapping).includes(r.toLowerCase()) ? t.mapping[r.toLowerCase()] : `${(0, _upperCaseFirst.upperCaseFirst)(r.toLowerCase())}`).join(" ");
23
+ return e.split(/\s+-\s+/).map(n => s(n)).join(" - ");
20
24
  }
@@ -28,13 +28,23 @@ export interface TitleCaseOptions {
28
28
  */
29
29
  mapping?: Record<string, string>;
30
30
  }
31
+ /**
32
+ * Check if the input string is in title case.
33
+ *
34
+ * @remarks
35
+ * Title case is defined as a string where each word is separated by spaces, and starts with an uppercase letter followed by lowercase letters - "This Is An Example".
36
+ *
37
+ * @param input - The input string to check.
38
+ * @returns True if the input is in title case, false otherwise.
39
+ */
40
+ export declare function isTitleCase(input: string | undefined): boolean;
31
41
  /**
32
42
  * Convert the input string to title case.
33
43
  *
34
44
  * @remarks
35
- * "This Is An Example"
45
+ * Title case is defined as a string where each word is separated by spaces, and starts with an uppercase letter followed by lowercase letters - "This Is An Example".
36
46
  *
37
47
  * @param input - The input string.
38
48
  * @returns The title cased string.
39
49
  */
40
- export declare function titleCase<T extends string | undefined>(input?: T, options?: TitleCaseOptions): T;
50
+ export declare function titleCase<T extends string | undefined>(input: T, options?: TitleCaseOptions): T;
@@ -1 +1 @@
1
- import{ACRONYMS as a}from"./acronyms";import{upperCaseFirst as o}from"./upper-case-first";export const FORMAT_MAPPING=a.reduce((s,e)=>(s[e.toLowerCase()]=e,s),{cspell:"CSpell",eslint:"ESLint",nx:"Nx"}),LOWER_CASE_WHEN_NOT_FIRST=["a","an","the","is","are","of","and","to","in","for","on","with","as","at","by"];export function titleCase(s,e={}){if(!s)return s;const n=r=>r.toLowerCase().split(/[\s\-_]+/).filter(Boolean).map((t,i)=>!e.skipLowerCaseWhenNotFirst&&LOWER_CASE_WHEN_NOT_FIRST.includes(t.toLowerCase())&&i>0?t.toLowerCase():!e.skipFormatMapping&&Object.keys(FORMAT_MAPPING).includes(t.toLowerCase())?FORMAT_MAPPING[t.toLowerCase()]:e.mapping&&Object.keys(e.mapping).includes(t.toLowerCase())?e.mapping[t.toLowerCase()]:`${o(t.toLowerCase())}`).join(" ");return s.split(/\s+-\s+/).map(r=>n(r)).join(" - ")}
1
+ import{ACRONYMS as o}from"./acronyms";import{upperCaseFirst as a}from"./upper-case-first";export const FORMAT_MAPPING=o.reduce((e,t)=>(e[t.toLowerCase()]=t,e),{cspell:"CSpell",eslint:"ESLint",nx:"Nx"}),LOWER_CASE_WHEN_NOT_FIRST=["a","an","the","is","are","of","and","to","in","for","on","with","as","at","by"];export function isTitleCase(e){return e?e.split(/[\s\-_]+/).filter(Boolean).every((s,n)=>Object.values(FORMAT_MAPPING).includes(s)||n>0&&LOWER_CASE_WHEN_NOT_FIRST.includes(s.toLowerCase())&&s===s.toLowerCase()?!0:/^[A-Z][a-z0-9]*$/.test(s)):!1}export function titleCase(e,t={}){if(isTitleCase(e)||e===void 0)return e;const s=n=>n.toLowerCase().split(/[\s\-_]+/).filter(Boolean).map((r,i)=>!t.skipLowerCaseWhenNotFirst&&LOWER_CASE_WHEN_NOT_FIRST.includes(r.toLowerCase())&&i>0?r.toLowerCase():!t.skipFormatMapping&&Object.keys(FORMAT_MAPPING).includes(r.toLowerCase())?FORMAT_MAPPING[r.toLowerCase()]:t.mapping&&Object.keys(t.mapping).includes(r.toLowerCase())?t.mapping[r.toLowerCase()]:`${a(r.toLowerCase())}`).join(" ");return e.split(/\s+-\s+/).map(n=>s(n)).join(" - ")}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/string-format",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "description": "A package containing utility functions to transform strings into various formats.",
6
6
  "repository": {
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "private": false,
12
12
  "publishConfig": { "access": "public" },
13
- "dependencies": { "@stryke/helpers": "^0.6.2", "@stryke/types": "^0.8.6" },
13
+ "dependencies": { "@stryke/helpers": "^0.7.1", "@stryke/types": "^0.8.6" },
14
14
  "devDependencies": {},
15
15
  "sideEffects": false,
16
16
  "files": ["dist/**/*"],
@@ -333,5 +333,5 @@
333
333
  "main": "./dist/index.cjs",
334
334
  "module": "./dist/index.mjs",
335
335
  "types": "./dist/index.d.ts",
336
- "gitHead": "b72d8a5bebd6ce47e458809cab4392bb000ecd81"
336
+ "gitHead": "57da58e3434302b35f798674d9f18f92ca7f7ac9"
337
337
  }