caseparser 5.0.0 → 5.1.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.
package/dist/index.js CHANGED
@@ -1676,6 +1676,12 @@ function capitalize(word) {
1676
1676
  while (i < word.length && isSymbol(word[i])) i++;
1677
1677
  return word.slice(0, i) + word.charAt(i).toUpperCase() + word.slice(i + 1);
1678
1678
  }
1679
+ /** Changes the case of the whole string, or keeps it when `transform` is `undefined`. */
1680
+ function applyTransform(str, transform) {
1681
+ if (transform === "lowercase") return str.toLowerCase();
1682
+ if (transform === "uppercase") return str.toUpperCase();
1683
+ return str;
1684
+ }
1679
1685
  const ToCamel = (str, keep) => words(str, keep).map((w, i) => i ? capitalize(w) : w).join("");
1680
1686
  const ToPascal = (str, keep) => words(str, keep).map(capitalize).join("");
1681
1687
  const ToSnake = (str, keep) => words(str, keep).join("_");
@@ -1683,255 +1689,74 @@ const ToKebab = (str, keep) => words(str, keep).join("-");
1683
1689
  const ToUpperSnake = (str, keep) => words(str, keep).join("_").toUpperCase();
1684
1690
  const ToUpperKebab = (str, keep) => words(str, keep).join("-").toUpperCase();
1685
1691
  const ToTrain = (str, keep) => words(str, keep).map(capitalize).join("-");
1686
- const ToDot = (str, keep) => words(str, keep, false).join(".");
1692
+ const ToDot = (str, keep, transform) => applyTransform(words(str, keep, false).join("."), transform);
1687
1693
  const ToTitle = (str, keep) => words(str, keep).map(capitalize).join(" ");
1688
1694
  const ToSentence = (str, keep) => capitalize(words(str, keep).join(" "));
1689
1695
  const ToPascalSnake = (str, keep) => words(str, keep).map(capitalize).join("_");
1690
- const ToPath = (str, keep) => words(str, keep, false).join("/");
1696
+ const ToPath = (str, keep, transform, separator = "/") => applyTransform(words(str.replace(/[/\\]/g, " "), keep, false).join(separator), transform);
1691
1697
  const ToSpace = (str, keep) => words(str, keep, false).join(" ");
1692
1698
  const ToLower = (str, keep) => words(str, keep).join(" ");
1693
1699
  const ToUpper = (str, keep) => words(str, keep).join(" ").toUpperCase();
1694
1700
  //#endregion
1695
1701
  //#region src/anyCase/anyCase.ts
1696
- /**
1697
- * Converts a string, or the keys of an object/array (deeply), from any case to camelCase.
1698
- *
1699
- * @example
1700
- * ```ts
1701
- * toCamel('hello_world'); // 'helloWorld'
1702
- * toCamel({ first_name: 'John', userID: 1 }); // { firstName: 'John', userId: 1 }
1703
- * toCamel('$hello-world', true); // '$helloWorld'
1704
- * ```
1705
- *
1706
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1707
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1708
- */
1709
- function toCamel(input, keepSymbols) {
1710
- return convert(input, (key) => ToCamel(key, keepSymbols));
1702
+ /** Reads the second argument of a `toX` function: the `allowSymbols` shorthand, or an options object. */
1703
+ function parseOptions(options) {
1704
+ if (typeof options === "object" && options !== null && !Array.isArray(options)) {
1705
+ const { allowSymbols, transform, separator } = options;
1706
+ return {
1707
+ allowSymbols,
1708
+ transform,
1709
+ separator
1710
+ };
1711
+ }
1712
+ return { allowSymbols: options };
1711
1713
  }
1712
- /**
1713
- * Converts a string, or the keys of an object/array (deeply), from any case to PascalCase.
1714
- *
1715
- * @example
1716
- * ```ts
1717
- * toPascal('hello-world'); // 'HelloWorld'
1718
- * toPascal({ 'first-name': 'John', userID: 1 }); // { FirstName: 'John', UserId: 1 }
1719
- * toPascal('$hello-world', true); // '$HelloWorld'
1720
- * ```
1721
- *
1722
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1723
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1724
- */
1725
- function toPascal(input, keepSymbols) {
1726
- return convert(input, (key) => ToPascal(key, keepSymbols));
1714
+ function toCamel(input, options) {
1715
+ return convert(input, (key) => ToCamel(key, parseOptions(options).allowSymbols));
1727
1716
  }
1728
- /**
1729
- * Converts a string, or the keys of an object/array (deeply), from any case to snake_case.
1730
- *
1731
- * @example
1732
- * ```ts
1733
- * toSnake('helloWorld'); // 'hello_world'
1734
- * toSnake({ firstName: 'John', userID: 1 }); // { first_name: 'John', user_id: 1 }
1735
- * toSnake('$hello-world', true); // '$hello_world'
1736
- * ```
1737
- *
1738
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1739
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1740
- */
1741
- function toSnake(input, keepSymbols) {
1742
- return convert(input, (key) => ToSnake(key, keepSymbols));
1717
+ function toPascal(input, options) {
1718
+ return convert(input, (key) => ToPascal(key, parseOptions(options).allowSymbols));
1743
1719
  }
1744
- /**
1745
- * Converts a string, or the keys of an object/array (deeply), from any case to kebab-case.
1746
- *
1747
- * @example
1748
- * ```ts
1749
- * toKebab('HELLO_WORLD'); // 'hello-world'
1750
- * toKebab({ FIRST_NAME: 'John', userID: 1 }); // { 'first-name': 'John', 'user-id': 1 }
1751
- * toKebab('$hello-world', true); // '$hello-world'
1752
- * ```
1753
- *
1754
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1755
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1756
- */
1757
- function toKebab(input, keepSymbols) {
1758
- return convert(input, (key) => ToKebab(key, keepSymbols));
1720
+ function toSnake(input, options) {
1721
+ return convert(input, (key) => ToSnake(key, parseOptions(options).allowSymbols));
1759
1722
  }
1760
- /**
1761
- * Converts a string, or the keys of an object/array (deeply), from any case to UPPER_SNAKE_CASE.
1762
- *
1763
- * @example
1764
- * ```ts
1765
- * toUpperSnake('hello-world'); // 'HELLO_WORLD'
1766
- * toUpperSnake({ 'first-name': 'John', userID: 1 }); // { FIRST_NAME: 'John', USER_ID: 1 }
1767
- * toUpperSnake('$hello-world', true); // '$HELLO_WORLD'
1768
- * ```
1769
- *
1770
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1771
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1772
- */
1773
- function toUpperSnake(input, keepSymbols) {
1774
- return convert(input, (key) => ToUpperSnake(key, keepSymbols));
1723
+ function toKebab(input, options) {
1724
+ return convert(input, (key) => ToKebab(key, parseOptions(options).allowSymbols));
1775
1725
  }
1776
- /**
1777
- * Converts a string, or the keys of an object/array (deeply), from any case to UPPER-KEBAB-CASE.
1778
- *
1779
- * @example
1780
- * ```ts
1781
- * toUpperKebab('helloWorld'); // 'HELLO-WORLD'
1782
- * toUpperKebab({ firstName: 'John', userID: 1 }); // { 'FIRST-NAME': 'John', 'USER-ID': 1 }
1783
- * toUpperKebab('$hello-world', true); // '$HELLO-WORLD'
1784
- * ```
1785
- *
1786
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1787
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1788
- */
1789
- function toUpperKebab(input, keepSymbols) {
1790
- return convert(input, (key) => ToUpperKebab(key, keepSymbols));
1726
+ function toUpperSnake(input, options) {
1727
+ return convert(input, (key) => ToUpperSnake(key, parseOptions(options).allowSymbols));
1791
1728
  }
1792
- /**
1793
- * Converts a string, or the keys of an object/array (deeply), from any case to Train-Case.
1794
- *
1795
- * @example
1796
- * ```ts
1797
- * toTrain('hello_world'); // 'Hello-World'
1798
- * toTrain({ first_name: 'John', userID: 1 }); // { 'First-Name': 'John', 'User-Id': 1 }
1799
- * toTrain('$hello-world', true); // '$Hello-World'
1800
- * ```
1801
- *
1802
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1803
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1804
- */
1805
- function toTrain(input, keepSymbols) {
1806
- return convert(input, (key) => ToTrain(key, keepSymbols));
1729
+ function toUpperKebab(input, options) {
1730
+ return convert(input, (key) => ToUpperKebab(key, parseOptions(options).allowSymbols));
1807
1731
  }
1808
- /**
1809
- * Converts a string, or the keys of an object/array (deeply), from any case to dot.case, keeping the original case of each word.
1810
- *
1811
- * @example
1812
- * ```ts
1813
- * toDot('HelloWorld'); // 'Hello.World'
1814
- * toDot({ FirstName: 'John', userID: 1 }); // { 'First.Name': 'John', 'user.ID': 1 }
1815
- * toDot('$hello-world', true); // '$hello.world'
1816
- * ```
1817
- *
1818
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1819
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1820
- */
1821
- function toDot(input, keepSymbols) {
1822
- return convert(input, (key) => ToDot(key, keepSymbols));
1732
+ function toTrain(input, options) {
1733
+ return convert(input, (key) => ToTrain(key, parseOptions(options).allowSymbols));
1823
1734
  }
1824
- /**
1825
- * Converts a string, or the keys of an object/array (deeply), from any case to Title Case.
1826
- *
1827
- * @example
1828
- * ```ts
1829
- * toTitle('hello_world'); // 'Hello World'
1830
- * toTitle({ first_name: 'John', userID: 1 }); // { 'First Name': 'John', 'User Id': 1 }
1831
- * toTitle('$hello-world', true); // '$Hello World'
1832
- * ```
1833
- *
1834
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1835
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1836
- */
1837
- function toTitle(input, keepSymbols) {
1838
- return convert(input, (key) => ToTitle(key, keepSymbols));
1735
+ function toDot(input, options) {
1736
+ const { allowSymbols, transform } = parseOptions(options);
1737
+ return convert(input, (key) => ToDot(key, allowSymbols, transform));
1839
1738
  }
1840
- /**
1841
- * Converts a string, or the keys of an object/array (deeply), from any case to Sentence case.
1842
- *
1843
- * @example
1844
- * ```ts
1845
- * toSentence('helloWorld'); // 'Hello world'
1846
- * toSentence({ firstName: 'John', userID: 1 }); // { 'First name': 'John', 'User id': 1 }
1847
- * toSentence('$hello-world', true); // '$Hello world'
1848
- * ```
1849
- *
1850
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1851
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1852
- */
1853
- function toSentence(input, keepSymbols) {
1854
- return convert(input, (key) => ToSentence(key, keepSymbols));
1739
+ function toTitle(input, options) {
1740
+ return convert(input, (key) => ToTitle(key, parseOptions(options).allowSymbols));
1855
1741
  }
1856
- /**
1857
- * Converts a string, or the keys of an object/array (deeply), from any case to Pascal_Snake_Case (also known as Ada_Case).
1858
- *
1859
- * @example
1860
- * ```ts
1861
- * toPascalSnake('helloWorld'); // 'Hello_World'
1862
- * toPascalSnake({ first_name: 'John', userID: 1 }); // { First_Name: 'John', User_Id: 1 }
1863
- * toPascalSnake('$hello-world', true); // '$Hello_World'
1864
- * ```
1865
- *
1866
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1867
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1868
- */
1869
- function toPascalSnake(input, keepSymbols) {
1870
- return convert(input, (key) => ToPascalSnake(key, keepSymbols));
1742
+ function toSentence(input, options) {
1743
+ return convert(input, (key) => ToSentence(key, parseOptions(options).allowSymbols));
1871
1744
  }
1872
- /**
1873
- * Converts a string, or the keys of an object/array (deeply), from any case to path/case, keeping the original case of each word.
1874
- *
1875
- * @example
1876
- * ```ts
1877
- * toPath('helloWorld'); // 'hello/World'
1878
- * toPath({ first_name: 'John', userID: 1 }); // { 'first/name': 'John', 'user/ID': 1 }
1879
- * toPath('$hello-world', true); // '$hello/world'
1880
- * ```
1881
- *
1882
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1883
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1884
- */
1885
- function toPath(input, keepSymbols) {
1886
- return convert(input, (key) => ToPath(key, keepSymbols));
1745
+ function toPascalSnake(input, options) {
1746
+ return convert(input, (key) => ToPascalSnake(key, parseOptions(options).allowSymbols));
1887
1747
  }
1888
- /**
1889
- * Converts a string, or the keys of an object/array (deeply), from any case to space case, keeping the original case of each word.
1890
- *
1891
- * @example
1892
- * ```ts
1893
- * toSpace('helloWorld'); // 'hello World'
1894
- * toSpace({ first_name: 'John', userID: 1 }); // { 'first name': 'John', 'user ID': 1 }
1895
- * toSpace('$hello-world', true); // '$hello world'
1896
- * ```
1897
- *
1898
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1899
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1900
- */
1901
- function toSpace(input, keepSymbols) {
1902
- return convert(input, (key) => ToSpace(key, keepSymbols));
1748
+ function toPath(input, options) {
1749
+ const { allowSymbols, transform, separator } = parseOptions(options);
1750
+ return convert(input, (key) => ToPath(key, allowSymbols, transform, separator === "\\" ? "\\" : "/"));
1903
1751
  }
1904
- /**
1905
- * Converts a string, or the keys of an object/array (deeply), from any case to lower case (space separated).
1906
- *
1907
- * @example
1908
- * ```ts
1909
- * toLower('helloWorld'); // 'hello world'
1910
- * toLower({ first_name: 'John', userID: 1 }); // { 'first name': 'John', 'user id': 1 }
1911
- * toLower('$hello-world', true); // '$hello world'
1912
- * ```
1913
- *
1914
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1915
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1916
- */
1917
- function toLower(input, keepSymbols) {
1918
- return convert(input, (key) => ToLower(key, keepSymbols));
1752
+ function toSpace(input, options) {
1753
+ return convert(input, (key) => ToSpace(key, parseOptions(options).allowSymbols));
1919
1754
  }
1920
- /**
1921
- * Converts a string, or the keys of an object/array (deeply), from any case to UPPER CASE (space separated).
1922
- *
1923
- * @example
1924
- * ```ts
1925
- * toUpper('helloWorld'); // 'HELLO WORLD'
1926
- * toUpper({ first_name: 'John', userID: 1 }); // { 'FIRST NAME': 'John', 'USER ID': 1 }
1927
- * toUpper('$hello-world', true); // '$HELLO WORLD'
1928
- * ```
1929
- *
1930
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1931
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1932
- */
1933
- function toUpper(input, keepSymbols) {
1934
- return convert(input, (key) => ToUpper(key, keepSymbols));
1755
+ function toLower(input, options) {
1756
+ return convert(input, (key) => ToLower(key, parseOptions(options).allowSymbols));
1757
+ }
1758
+ function toUpper(input, options) {
1759
+ return convert(input, (key) => ToUpper(key, parseOptions(options).allowSymbols));
1935
1760
  }
1936
1761
  //#endregion
1937
1762
  export { camelToDash, camelToDot, camelToPascal, camelToSentence, camelToSnake, camelToTitle, camelToTrain, camelToUpperDash, camelToUpperSnake, dashToCamel, dashToDot, dashToPascal, dashToSentence, dashToSnake, dashToTitle, dashToTrain, dashToUpperDash, dashToUpperSnake, dotToCamel, dotToDash, dotToPascal, dotToSentence, dotToSnake, dotToTitle, dotToTrain, dotToUpperDash, dotToUpperSnake, pascalToCamel, pascalToDash, pascalToDot, pascalToSentence, pascalToSnake, pascalToTitle, pascalToTrain, pascalToUpperDash, pascalToUpperSnake, sentenceToCamel, sentenceToDash, sentenceToDot, sentenceToPascal, sentenceToSnake, sentenceToTitle, sentenceToTrain, sentenceToUpperDash, sentenceToUpperSnake, snakeToCamel, snakeToDash, snakeToDot, snakeToPascal, snakeToSentence, snakeToTitle, snakeToTrain, snakeToUpperDash, snakeToUpperSnake, titleToCamel, titleToDash, titleToDot, titleToPascal, titleToSentence, titleToSnake, titleToTrain, titleToUpperDash, titleToUpperSnake, toCamel, toDot, toKebab, toLower, toPascal, toPascalSnake, toPath, toSentence, toSnake, toSpace, toTitle, toTrain, toUpper, toUpperKebab, toUpperSnake, trainToCamel, trainToDash, trainToDot, trainToPascal, trainToSentence, trainToSnake, trainToTitle, trainToUpperDash, trainToUpperSnake, upperDashToCamel, upperDashToDash, upperDashToDot, upperDashToPascal, upperDashToSentence, upperDashToSnake, upperDashToTitle, upperDashToTrain, upperDashToUpperSnake, upperSnakeToCamel, upperSnakeToDash, upperSnakeToDot, upperSnakeToPascal, upperSnakeToSentence, upperSnakeToSnake, upperSnakeToTitle, upperSnakeToTrain, upperSnakeToUpperDash };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "caseparser",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "Convert Strings/Arrays/Objects from a type of case to another one.",
5
5
  "author": "Fernando Machado Bernardino <fernandombernardino@gmail.com>",
6
6
  "homepage": "https://github.com/NandoMB/caseparser#readme",
@@ -44,20 +44,28 @@
44
44
  "coverage": "vitest run --project node --coverage",
45
45
  "build": "tsdown",
46
46
  "lint": "tsc",
47
- "ci": "pnpm run lint && pnpm run test && pnpm run build",
47
+ "ci": "pnpm run lint && pnpm run test && pnpm run build && pnpm run lint:package",
48
48
  "release": "node scripts/release.js",
49
49
  "changeset:version": "changeset version && node scripts/sync-jsr-version.js",
50
+ "lint:package": "publint && attw --pack .",
50
51
  "test:browser": "vitest run --project browser",
51
- "test:examples": "node scripts/test-examples.js"
52
+ "test:bun": "bunx --bun vitest run --project node",
53
+ "test:deno": "deno run -A node_modules/vitest/vitest.mjs run --project node",
54
+ "test:ts-legacy": "node node_modules/typescript-4.1/bin/tsc -p test/typescript-4.1",
55
+ "lint:jsr": "deno publish --dry-run --allow-dirty",
56
+ "test:ci": "pnpm run ci && pnpm run test:ts-legacy && pnpm run test:browser && pnpm run test:bun && pnpm run test:deno && pnpm run lint:jsr"
52
57
  },
53
58
  "devDependencies": {
59
+ "@arethetypeswrong/cli": "^0.18.5",
54
60
  "@changesets/cli": "^3.0.3",
55
61
  "@types/node": "^26.6.2",
56
62
  "@vitest/browser-playwright": "^5.0.1",
57
63
  "@vitest/coverage-istanbul": "^5.0.1",
58
64
  "playwright": "^1.63.0",
65
+ "publint": "^0.3.24",
59
66
  "tsdown": "^0.23.0",
60
67
  "typescript": "^7.0.2",
68
+ "typescript-4.1": "npm:typescript@4.1.6",
61
69
  "vitest": "^5.0.1"
62
70
  },
63
71
  "keywords": [