@stryke/string-format 0.4.11 → 0.5.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.
@@ -3,11 +3,18 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.LOWER_CASE_WHEN_NOT_FIRST = exports.FORMAT_MAPPING = void 0;
6
7
  exports.titleCase = titleCase;
7
8
  var _acronyms = require("./acronyms.cjs");
8
9
  var _upperCaseFirst = require("./upper-case-first.cjs");
9
- function titleCase(e) {
10
- if (!e) return e;
11
- const s = t => t.toLowerCase().split(/[\s\-_]+/).filter(Boolean).map(r => _acronyms.ACRONYMS.includes(r.toUpperCase()) ? r.toUpperCase() : `${(0, _upperCaseFirst.upperCaseFirst)(r.toLowerCase())}`).join(" ");
12
- return e.split(/\s+-\s+/).map(t => s(t)).join(" - ");
10
+ const FORMAT_MAPPING = exports.FORMAT_MAPPING = _acronyms.ACRONYMS.reduce((s, e) => (s[e.toLowerCase()] = e, s), {
11
+ cspell: "CSpell",
12
+ eslint: "ESLint",
13
+ nx: "Nx"
14
+ }),
15
+ 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(" - ");
13
20
  }
@@ -1,3 +1,33 @@
1
+ export declare const FORMAT_MAPPING: Record<string, string>;
2
+ export declare const LOWER_CASE_WHEN_NOT_FIRST: string[];
3
+ export interface TitleCaseOptions {
4
+ /**
5
+ * If true, skip the format mapping. This will skip the conversion of known acronyms to their upper case form.
6
+ *
7
+ * @remarks
8
+ * The current list of word format mappings is stored in {@link FORMAT_MAPPING}.
9
+ *
10
+ * @defaultValue false
11
+ */
12
+ skipFormatMapping?: boolean;
13
+ /**
14
+ * If true, lower case words that are not the first word in the title.
15
+ *
16
+ * @remarks
17
+ * The current list of words that are lower cased when not first are stored in {@link LOWER_CASE_WHEN_NOT_FIRST}.
18
+ *
19
+ *
20
+ * @defaultValue false
21
+ */
22
+ skipLowerCaseWhenNotFirst?: boolean;
23
+ /**
24
+ * A custom mapping of words to their formatted versions.
25
+ *
26
+ * @remarks
27
+ * This allows you to provide your own mappings for specific words that should be formatted in a certain way.
28
+ */
29
+ mapping?: Record<string, string>;
30
+ }
1
31
  /**
2
32
  * Convert the input string to title case.
3
33
  *
@@ -7,4 +37,4 @@
7
37
  * @param input - The input string.
8
38
  * @returns The title cased string.
9
39
  */
10
- export declare function titleCase<T extends string | undefined>(input?: T): T;
40
+ export declare function titleCase<T extends string | undefined>(input?: T, options?: TitleCaseOptions): T;
@@ -1 +1 @@
1
- import{ACRONYMS as o}from"./acronyms";import{upperCaseFirst as n}from"./upper-case-first";export function titleCase(e){if(!e)return e;const s=t=>t.toLowerCase().split(/[\s\-_]+/).filter(Boolean).map(r=>o.includes(r.toUpperCase())?r.toUpperCase():`${n(r.toLowerCase())}`).join(" ");return e.split(/\s+-\s+/).map(t=>s(t)).join(" - ")}
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(" - ")}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/string-format",
3
- "version": "0.4.11",
3
+ "version": "0.5.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.1", "@stryke/types": "^0.8.4" },
13
+ "dependencies": { "@stryke/helpers": "^0.6.2", "@stryke/types": "^0.8.5" },
14
14
  "devDependencies": {},
15
15
  "sideEffects": false,
16
16
  "files": ["dist/**/*"],