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.cjs CHANGED
@@ -1677,6 +1677,12 @@ function capitalize(word) {
1677
1677
  while (i < word.length && isSymbol(word[i])) i++;
1678
1678
  return word.slice(0, i) + word.charAt(i).toUpperCase() + word.slice(i + 1);
1679
1679
  }
1680
+ /** Changes the case of the whole string, or keeps it when `transform` is `undefined`. */
1681
+ function applyTransform(str, transform) {
1682
+ if (transform === "lowercase") return str.toLowerCase();
1683
+ if (transform === "uppercase") return str.toUpperCase();
1684
+ return str;
1685
+ }
1680
1686
  const ToCamel = (str, keep) => words(str, keep).map((w, i) => i ? capitalize(w) : w).join("");
1681
1687
  const ToPascal = (str, keep) => words(str, keep).map(capitalize).join("");
1682
1688
  const ToSnake = (str, keep) => words(str, keep).join("_");
@@ -1684,255 +1690,74 @@ const ToKebab = (str, keep) => words(str, keep).join("-");
1684
1690
  const ToUpperSnake = (str, keep) => words(str, keep).join("_").toUpperCase();
1685
1691
  const ToUpperKebab = (str, keep) => words(str, keep).join("-").toUpperCase();
1686
1692
  const ToTrain = (str, keep) => words(str, keep).map(capitalize).join("-");
1687
- const ToDot = (str, keep) => words(str, keep, false).join(".");
1693
+ const ToDot = (str, keep, transform) => applyTransform(words(str, keep, false).join("."), transform);
1688
1694
  const ToTitle = (str, keep) => words(str, keep).map(capitalize).join(" ");
1689
1695
  const ToSentence = (str, keep) => capitalize(words(str, keep).join(" "));
1690
1696
  const ToPascalSnake = (str, keep) => words(str, keep).map(capitalize).join("_");
1691
- const ToPath = (str, keep) => words(str, keep, false).join("/");
1697
+ const ToPath = (str, keep, transform, separator = "/") => applyTransform(words(str.replace(/[/\\]/g, " "), keep, false).join(separator), transform);
1692
1698
  const ToSpace = (str, keep) => words(str, keep, false).join(" ");
1693
1699
  const ToLower = (str, keep) => words(str, keep).join(" ");
1694
1700
  const ToUpper = (str, keep) => words(str, keep).join(" ").toUpperCase();
1695
1701
  //#endregion
1696
1702
  //#region src/anyCase/anyCase.ts
1697
- /**
1698
- * Converts a string, or the keys of an object/array (deeply), from any case to camelCase.
1699
- *
1700
- * @example
1701
- * ```ts
1702
- * toCamel('hello_world'); // 'helloWorld'
1703
- * toCamel({ first_name: 'John', userID: 1 }); // { firstName: 'John', userId: 1 }
1704
- * toCamel('$hello-world', true); // '$helloWorld'
1705
- * ```
1706
- *
1707
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1708
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1709
- */
1710
- function toCamel(input, keepSymbols) {
1711
- return convert(input, (key) => ToCamel(key, keepSymbols));
1703
+ /** Reads the second argument of a `toX` function: the `allowSymbols` shorthand, or an options object. */
1704
+ function parseOptions(options) {
1705
+ if (typeof options === "object" && options !== null && !Array.isArray(options)) {
1706
+ const { allowSymbols, transform, separator } = options;
1707
+ return {
1708
+ allowSymbols,
1709
+ transform,
1710
+ separator
1711
+ };
1712
+ }
1713
+ return { allowSymbols: options };
1712
1714
  }
1713
- /**
1714
- * Converts a string, or the keys of an object/array (deeply), from any case to PascalCase.
1715
- *
1716
- * @example
1717
- * ```ts
1718
- * toPascal('hello-world'); // 'HelloWorld'
1719
- * toPascal({ 'first-name': 'John', userID: 1 }); // { FirstName: 'John', UserId: 1 }
1720
- * toPascal('$hello-world', true); // '$HelloWorld'
1721
- * ```
1722
- *
1723
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1724
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1725
- */
1726
- function toPascal(input, keepSymbols) {
1727
- return convert(input, (key) => ToPascal(key, keepSymbols));
1715
+ function toCamel(input, options) {
1716
+ return convert(input, (key) => ToCamel(key, parseOptions(options).allowSymbols));
1728
1717
  }
1729
- /**
1730
- * Converts a string, or the keys of an object/array (deeply), from any case to snake_case.
1731
- *
1732
- * @example
1733
- * ```ts
1734
- * toSnake('helloWorld'); // 'hello_world'
1735
- * toSnake({ firstName: 'John', userID: 1 }); // { first_name: 'John', user_id: 1 }
1736
- * toSnake('$hello-world', true); // '$hello_world'
1737
- * ```
1738
- *
1739
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1740
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1741
- */
1742
- function toSnake(input, keepSymbols) {
1743
- return convert(input, (key) => ToSnake(key, keepSymbols));
1718
+ function toPascal(input, options) {
1719
+ return convert(input, (key) => ToPascal(key, parseOptions(options).allowSymbols));
1744
1720
  }
1745
- /**
1746
- * Converts a string, or the keys of an object/array (deeply), from any case to kebab-case.
1747
- *
1748
- * @example
1749
- * ```ts
1750
- * toKebab('HELLO_WORLD'); // 'hello-world'
1751
- * toKebab({ FIRST_NAME: 'John', userID: 1 }); // { 'first-name': 'John', 'user-id': 1 }
1752
- * toKebab('$hello-world', true); // '$hello-world'
1753
- * ```
1754
- *
1755
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1756
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1757
- */
1758
- function toKebab(input, keepSymbols) {
1759
- return convert(input, (key) => ToKebab(key, keepSymbols));
1721
+ function toSnake(input, options) {
1722
+ return convert(input, (key) => ToSnake(key, parseOptions(options).allowSymbols));
1760
1723
  }
1761
- /**
1762
- * Converts a string, or the keys of an object/array (deeply), from any case to UPPER_SNAKE_CASE.
1763
- *
1764
- * @example
1765
- * ```ts
1766
- * toUpperSnake('hello-world'); // 'HELLO_WORLD'
1767
- * toUpperSnake({ 'first-name': 'John', userID: 1 }); // { FIRST_NAME: 'John', USER_ID: 1 }
1768
- * toUpperSnake('$hello-world', true); // '$HELLO_WORLD'
1769
- * ```
1770
- *
1771
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1772
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1773
- */
1774
- function toUpperSnake(input, keepSymbols) {
1775
- return convert(input, (key) => ToUpperSnake(key, keepSymbols));
1724
+ function toKebab(input, options) {
1725
+ return convert(input, (key) => ToKebab(key, parseOptions(options).allowSymbols));
1776
1726
  }
1777
- /**
1778
- * Converts a string, or the keys of an object/array (deeply), from any case to UPPER-KEBAB-CASE.
1779
- *
1780
- * @example
1781
- * ```ts
1782
- * toUpperKebab('helloWorld'); // 'HELLO-WORLD'
1783
- * toUpperKebab({ firstName: 'John', userID: 1 }); // { 'FIRST-NAME': 'John', 'USER-ID': 1 }
1784
- * toUpperKebab('$hello-world', true); // '$HELLO-WORLD'
1785
- * ```
1786
- *
1787
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1788
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1789
- */
1790
- function toUpperKebab(input, keepSymbols) {
1791
- return convert(input, (key) => ToUpperKebab(key, keepSymbols));
1727
+ function toUpperSnake(input, options) {
1728
+ return convert(input, (key) => ToUpperSnake(key, parseOptions(options).allowSymbols));
1792
1729
  }
1793
- /**
1794
- * Converts a string, or the keys of an object/array (deeply), from any case to Train-Case.
1795
- *
1796
- * @example
1797
- * ```ts
1798
- * toTrain('hello_world'); // 'Hello-World'
1799
- * toTrain({ first_name: 'John', userID: 1 }); // { 'First-Name': 'John', 'User-Id': 1 }
1800
- * toTrain('$hello-world', true); // '$Hello-World'
1801
- * ```
1802
- *
1803
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1804
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1805
- */
1806
- function toTrain(input, keepSymbols) {
1807
- return convert(input, (key) => ToTrain(key, keepSymbols));
1730
+ function toUpperKebab(input, options) {
1731
+ return convert(input, (key) => ToUpperKebab(key, parseOptions(options).allowSymbols));
1808
1732
  }
1809
- /**
1810
- * Converts a string, or the keys of an object/array (deeply), from any case to dot.case, keeping the original case of each word.
1811
- *
1812
- * @example
1813
- * ```ts
1814
- * toDot('HelloWorld'); // 'Hello.World'
1815
- * toDot({ FirstName: 'John', userID: 1 }); // { 'First.Name': 'John', 'user.ID': 1 }
1816
- * toDot('$hello-world', true); // '$hello.world'
1817
- * ```
1818
- *
1819
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1820
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1821
- */
1822
- function toDot(input, keepSymbols) {
1823
- return convert(input, (key) => ToDot(key, keepSymbols));
1733
+ function toTrain(input, options) {
1734
+ return convert(input, (key) => ToTrain(key, parseOptions(options).allowSymbols));
1824
1735
  }
1825
- /**
1826
- * Converts a string, or the keys of an object/array (deeply), from any case to Title Case.
1827
- *
1828
- * @example
1829
- * ```ts
1830
- * toTitle('hello_world'); // 'Hello World'
1831
- * toTitle({ first_name: 'John', userID: 1 }); // { 'First Name': 'John', 'User Id': 1 }
1832
- * toTitle('$hello-world', true); // '$Hello World'
1833
- * ```
1834
- *
1835
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1836
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1837
- */
1838
- function toTitle(input, keepSymbols) {
1839
- return convert(input, (key) => ToTitle(key, keepSymbols));
1736
+ function toDot(input, options) {
1737
+ const { allowSymbols, transform } = parseOptions(options);
1738
+ return convert(input, (key) => ToDot(key, allowSymbols, transform));
1840
1739
  }
1841
- /**
1842
- * Converts a string, or the keys of an object/array (deeply), from any case to Sentence case.
1843
- *
1844
- * @example
1845
- * ```ts
1846
- * toSentence('helloWorld'); // 'Hello world'
1847
- * toSentence({ firstName: 'John', userID: 1 }); // { 'First name': 'John', 'User id': 1 }
1848
- * toSentence('$hello-world', true); // '$Hello world'
1849
- * ```
1850
- *
1851
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1852
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1853
- */
1854
- function toSentence(input, keepSymbols) {
1855
- return convert(input, (key) => ToSentence(key, keepSymbols));
1740
+ function toTitle(input, options) {
1741
+ return convert(input, (key) => ToTitle(key, parseOptions(options).allowSymbols));
1856
1742
  }
1857
- /**
1858
- * Converts a string, or the keys of an object/array (deeply), from any case to Pascal_Snake_Case (also known as Ada_Case).
1859
- *
1860
- * @example
1861
- * ```ts
1862
- * toPascalSnake('helloWorld'); // 'Hello_World'
1863
- * toPascalSnake({ first_name: 'John', userID: 1 }); // { First_Name: 'John', User_Id: 1 }
1864
- * toPascalSnake('$hello-world', true); // '$Hello_World'
1865
- * ```
1866
- *
1867
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1868
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1869
- */
1870
- function toPascalSnake(input, keepSymbols) {
1871
- return convert(input, (key) => ToPascalSnake(key, keepSymbols));
1743
+ function toSentence(input, options) {
1744
+ return convert(input, (key) => ToSentence(key, parseOptions(options).allowSymbols));
1872
1745
  }
1873
- /**
1874
- * Converts a string, or the keys of an object/array (deeply), from any case to path/case, keeping the original case of each word.
1875
- *
1876
- * @example
1877
- * ```ts
1878
- * toPath('helloWorld'); // 'hello/World'
1879
- * toPath({ first_name: 'John', userID: 1 }); // { 'first/name': 'John', 'user/ID': 1 }
1880
- * toPath('$hello-world', true); // '$hello/world'
1881
- * ```
1882
- *
1883
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1884
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1885
- */
1886
- function toPath(input, keepSymbols) {
1887
- return convert(input, (key) => ToPath(key, keepSymbols));
1746
+ function toPascalSnake(input, options) {
1747
+ return convert(input, (key) => ToPascalSnake(key, parseOptions(options).allowSymbols));
1888
1748
  }
1889
- /**
1890
- * Converts a string, or the keys of an object/array (deeply), from any case to space case, keeping the original case of each word.
1891
- *
1892
- * @example
1893
- * ```ts
1894
- * toSpace('helloWorld'); // 'hello World'
1895
- * toSpace({ first_name: 'John', userID: 1 }); // { 'first name': 'John', 'user ID': 1 }
1896
- * toSpace('$hello-world', true); // '$hello world'
1897
- * ```
1898
- *
1899
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1900
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1901
- */
1902
- function toSpace(input, keepSymbols) {
1903
- return convert(input, (key) => ToSpace(key, keepSymbols));
1749
+ function toPath(input, options) {
1750
+ const { allowSymbols, transform, separator } = parseOptions(options);
1751
+ return convert(input, (key) => ToPath(key, allowSymbols, transform, separator === "\\" ? "\\" : "/"));
1904
1752
  }
1905
- /**
1906
- * Converts a string, or the keys of an object/array (deeply), from any case to lower case (space separated).
1907
- *
1908
- * @example
1909
- * ```ts
1910
- * toLower('helloWorld'); // 'hello world'
1911
- * toLower({ first_name: 'John', userID: 1 }); // { 'first name': 'John', 'user id': 1 }
1912
- * toLower('$hello-world', true); // '$hello world'
1913
- * ```
1914
- *
1915
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1916
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1917
- */
1918
- function toLower(input, keepSymbols) {
1919
- return convert(input, (key) => ToLower(key, keepSymbols));
1753
+ function toSpace(input, options) {
1754
+ return convert(input, (key) => ToSpace(key, parseOptions(options).allowSymbols));
1920
1755
  }
1921
- /**
1922
- * Converts a string, or the keys of an object/array (deeply), from any case to UPPER CASE (space separated).
1923
- *
1924
- * @example
1925
- * ```ts
1926
- * toUpper('helloWorld'); // 'HELLO WORLD'
1927
- * toUpper({ first_name: 'John', userID: 1 }); // { 'FIRST NAME': 'John', 'USER ID': 1 }
1928
- * toUpper('$hello-world', true); // '$HELLO WORLD'
1929
- * ```
1930
- *
1931
- * @param keepSymbols Symbols (ASCII punctuation, like `$` or `@`) are removed by default.
1932
- * Pass `true` to keep all of them, or an array to keep only some (e.g. `['$']`).
1933
- */
1934
- function toUpper(input, keepSymbols) {
1935
- return convert(input, (key) => ToUpper(key, keepSymbols));
1756
+ function toLower(input, options) {
1757
+ return convert(input, (key) => ToLower(key, parseOptions(options).allowSymbols));
1758
+ }
1759
+ function toUpper(input, options) {
1760
+ return convert(input, (key) => ToUpper(key, parseOptions(options).allowSymbols));
1936
1761
  }
1937
1762
  //#endregion
1938
1763
  exports.camelToDash = camelToDash;