yummies 6.7.1 → 6.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.
- package/format.cjs +12 -1
- package/format.cjs.map +1 -1
- package/format.d.ts +1 -0
- package/format.js +12 -1
- package/format.js.map +1 -1
- package/package.json +1 -1
- package/types.d.ts +6 -1
- package/types.global.d.ts +5 -0
package/format.cjs
CHANGED
|
@@ -25,7 +25,18 @@ const number = (rawValue, userSettings) => {
|
|
|
25
25
|
if (typeGuard.typeGuard.isNumber(value)) {
|
|
26
26
|
let raw = `${value}`;
|
|
27
27
|
if (digits !== false) {
|
|
28
|
-
|
|
28
|
+
if (settings.cropDigitsOnly) {
|
|
29
|
+
const [integerPart2, decimalPart2] = `${raw}`.split(".");
|
|
30
|
+
const leftPart = integerPart2;
|
|
31
|
+
const rightPart = (decimalPart2 || "").slice(0, digits).padEnd(digits, "0");
|
|
32
|
+
if (rightPart) {
|
|
33
|
+
raw = `${leftPart}.${rightPart}`;
|
|
34
|
+
} else {
|
|
35
|
+
raw = leftPart;
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
raw = value.toFixed(digits);
|
|
39
|
+
}
|
|
29
40
|
}
|
|
30
41
|
if (cutZeros) {
|
|
31
42
|
raw = `${+raw}`;
|
package/format.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.cjs","sources":["../src/format/constants.ts","../src/format/number.ts","../src/format/percent.ts","../src/format/skip-spaces.ts"],"sourcesContent":["export const NO_VALUE = '–'; // en-dash\n\nexport const HYPHEN = '-';\n\nexport const INFINITY = '∞';\n","import { parser } from 'yummies/parser';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\nimport { NO_VALUE } from './constants.js';\n\nexport interface NumberFormatSettings {\n delimiter?: string;\n /**\n * digitsOnlyForFloat - Show digits after decimal point only if they are not zeros after converting to number.\n * Example: \"0.00\" -> \"0\", \"0.10\" -> \"0.1\", but \"0.003\" -> \"0.003\"\n *\n * @default true\n */\n digitsOnlyForFloat?: boolean;\n /**\n * Text which will be returned if the value is undefined, null, NaN, Infinity or empty string.\n * Example: \"–\" will be returned if the value is undefined and emptyText is \"–\".\n */\n emptyText?: string;\n /**\n * Text to append to the end of the formatted number.\n * Example: if value is 1000 and postfix is \"₽\", result will be \"1 000₽\".\n */\n postfix?: string;\n /**\n * Fixed number of digits after the decimal point (number.toFixed() method)\n * If set to false, the truncation is ignored!\n */\n digits?: number | false;\n /**\n * Remove trailing zeros from the end of the number\n * Example: 0.010000000000000000000000000000000000000000000 -> 0.01\n */\n cutZeros?: boolean;\n}\n\nexport const number = (\n rawValue: Maybe<string | number>,\n userSettings?: Maybe<NumberFormatSettings>,\n): string => {\n const settings = {\n ...number.defaultSettings,\n ...userSettings,\n };\n\n const digits = settings.digits ?? 0;\n const cutZeros = settings?.cutZeros ?? false;\n const delimiter = settings.delimiter ?? ' ';\n const postfix = settings.postfix ?? '';\n const emptyText = settings.emptyText ?? NO_VALUE;\n const digitsOnlyForFloat = settings.digitsOnlyForFloat ?? true;\n\n let value: Maybe<number>;\n\n if (typeGuard.isString(rawValue)) {\n value = parser.number(value, { fallback: undefined });\n } else {\n value = rawValue;\n }\n\n if (typeGuard.isNumber(value)) {\n let raw: string = `${value}`;\n\n if (digits !== false) {\n raw = value.toFixed(digits);\n }\n if (cutZeros) {\n raw = `${+raw}`;\n }\n\n const [integerPart, decimalPart] = raw.split('.', 2);\n\n let formattedIntegerPart = integerPart;\n let formattedDecimalPart = '';\n\n if (decimalPart && (!digitsOnlyForFloat || !/^0+$/.test(decimalPart))) {\n formattedDecimalPart = `.${decimalPart}`;\n }\n\n const rgx = /(\\d+)(\\d{3})/;\n\n while (rgx.test(formattedIntegerPart) && delimiter) {\n formattedIntegerPart = formattedIntegerPart.replace(\n rgx,\n `$1${delimiter}$2`,\n );\n }\n\n return formattedIntegerPart + formattedDecimalPart + postfix;\n }\n\n return emptyText;\n};\n\nnumber.defaultSettings = {} as NumberFormatSettings;\n","import { parser } from 'yummies/parser';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\n\nimport { NO_VALUE } from './constants.js';\n\nexport interface PercentFormatSettings\n extends Omit<parser.NumberParserSettings, 'fallback'> {\n divider?: string;\n delimiter?: string;\n symbol?: string;\n emptyText?: string;\n}\n\n/**\n * 100 -> 100%\n * 99.123214412 -> 99.12%\n * 99.123214412 -> 99,12%\n */\nexport const percent = (\n value: Maybe<number | string>,\n settings?: PercentFormatSettings,\n) => {\n const numericValue = parser.number(value, {\n ...settings,\n digits: settings?.digits ?? 2,\n fallback: Number.NaN,\n });\n\n if (typeGuard.isNumber(numericValue)) {\n const divider = settings?.divider ?? '.';\n\n const formattedPercent =\n divider === '.' ? numericValue : `${numericValue}`.replace('.', divider);\n\n return `${formattedPercent}${settings?.delimiter ?? ''}${settings?.symbol ?? '%'}`;\n } else {\n return settings?.emptyText ?? NO_VALUE;\n }\n};\n","/**\n * Removes all spaces in string\n */\nexport const skipSpaces = (value: string) => value.replaceAll(/\\s/g, '');\n"],"names":["typeGuard","parser"],"mappings":";;;;AAAO,MAAM,WAAW;AAEjB,MAAM,SAAS;AAEf,MAAM,WAAW;
|
|
1
|
+
{"version":3,"file":"format.cjs","sources":["../src/format/constants.ts","../src/format/number.ts","../src/format/percent.ts","../src/format/skip-spaces.ts"],"sourcesContent":["export const NO_VALUE = '–'; // en-dash\n\nexport const HYPHEN = '-';\n\nexport const INFINITY = '∞';\n","import { parser } from 'yummies/parser';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\nimport { NO_VALUE } from './constants.js';\n\nexport interface NumberFormatSettings {\n delimiter?: string;\n /**\n * digitsOnlyForFloat - Show digits after decimal point only if they are not zeros after converting to number.\n * Example: \"0.00\" -> \"0\", \"0.10\" -> \"0.1\", but \"0.003\" -> \"0.003\"\n *\n * @default true\n */\n digitsOnlyForFloat?: boolean;\n /**\n * Text which will be returned if the value is undefined, null, NaN, Infinity or empty string.\n * Example: \"–\" will be returned if the value is undefined and emptyText is \"–\".\n */\n emptyText?: string;\n /**\n * Text to append to the end of the formatted number.\n * Example: if value is 1000 and postfix is \"₽\", result will be \"1 000₽\".\n */\n postfix?: string;\n /**\n * Fixed number of digits after the decimal point (number.toFixed() method)\n * If set to false, the truncation is ignored!\n */\n digits?: number | false;\n /**\n * Remove trailing zeros from the end of the number\n * Example: 0.010000000000000000000000000000000000000000000 -> 0.01\n */\n cutZeros?: boolean;\n cropDigitsOnly?: boolean;\n}\n\nexport const number = (\n rawValue: Maybe<string | number>,\n userSettings?: Maybe<NumberFormatSettings>,\n): string => {\n const settings = {\n ...number.defaultSettings,\n ...userSettings,\n };\n\n const digits = settings.digits ?? 0;\n const cutZeros = settings?.cutZeros ?? false;\n const delimiter = settings.delimiter ?? ' ';\n const postfix = settings.postfix ?? '';\n const emptyText = settings.emptyText ?? NO_VALUE;\n const digitsOnlyForFloat = settings.digitsOnlyForFloat ?? true;\n\n let value: Maybe<number>;\n\n if (typeGuard.isString(rawValue)) {\n value = parser.number(value, { fallback: undefined });\n } else {\n value = rawValue;\n }\n\n if (typeGuard.isNumber(value)) {\n let raw: string = `${value}`;\n\n if (digits !== false) {\n if (settings.cropDigitsOnly) {\n const [integerPart, decimalPart] = `${raw}`.split('.');\n const leftPart = integerPart;\n const rightPart = (decimalPart || '')\n .slice(0, digits)\n .padEnd(digits, '0');\n\n if (rightPart) {\n raw = `${leftPart}.${rightPart}`;\n } else {\n raw = leftPart;\n }\n } else {\n raw = value.toFixed(digits);\n }\n }\n\n if (cutZeros) {\n raw = `${+raw}`;\n }\n\n const [integerPart, decimalPart] = raw.split('.', 2);\n\n let formattedIntegerPart = integerPart;\n let formattedDecimalPart = '';\n\n if (decimalPart && (!digitsOnlyForFloat || !/^0+$/.test(decimalPart))) {\n formattedDecimalPart = `.${decimalPart}`;\n }\n\n const rgx = /(\\d+)(\\d{3})/;\n\n while (rgx.test(formattedIntegerPart) && delimiter) {\n formattedIntegerPart = formattedIntegerPart.replace(\n rgx,\n `$1${delimiter}$2`,\n );\n }\n\n return formattedIntegerPart + formattedDecimalPart + postfix;\n }\n\n return emptyText;\n};\n\nnumber.defaultSettings = {} as NumberFormatSettings;\n","import { parser } from 'yummies/parser';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\n\nimport { NO_VALUE } from './constants.js';\n\nexport interface PercentFormatSettings\n extends Omit<parser.NumberParserSettings, 'fallback'> {\n divider?: string;\n delimiter?: string;\n symbol?: string;\n emptyText?: string;\n}\n\n/**\n * 100 -> 100%\n * 99.123214412 -> 99.12%\n * 99.123214412 -> 99,12%\n */\nexport const percent = (\n value: Maybe<number | string>,\n settings?: PercentFormatSettings,\n) => {\n const numericValue = parser.number(value, {\n ...settings,\n digits: settings?.digits ?? 2,\n fallback: Number.NaN,\n });\n\n if (typeGuard.isNumber(numericValue)) {\n const divider = settings?.divider ?? '.';\n\n const formattedPercent =\n divider === '.' ? numericValue : `${numericValue}`.replace('.', divider);\n\n return `${formattedPercent}${settings?.delimiter ?? ''}${settings?.symbol ?? '%'}`;\n } else {\n return settings?.emptyText ?? NO_VALUE;\n }\n};\n","/**\n * Removes all spaces in string\n */\nexport const skipSpaces = (value: string) => value.replaceAll(/\\s/g, '');\n"],"names":["typeGuard","parser","integerPart","decimalPart"],"mappings":";;;;AAAO,MAAM,WAAW;AAEjB,MAAM,SAAS;AAEf,MAAM,WAAW;ACiCjB,MAAM,SAAS,CACpB,UACA,iBACW;AACX,QAAM,WAAW;AAAA,IACf,GAAG,OAAO;AAAA,IACV,GAAG;AAAA,EAAA;AAGL,QAAM,SAAS,SAAS,UAAU;AAClC,QAAM,WAAW,UAAU,YAAY;AACvC,QAAM,YAAY,SAAS,aAAa;AACxC,QAAM,UAAU,SAAS,WAAW;AACpC,QAAM,YAAY,SAAS,aAAa;AACxC,QAAM,qBAAqB,SAAS,sBAAsB;AAE1D,MAAI;AA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AC5B,UAAU,OAAO;AAAA,EAAA,CAClB;AAED,MAAID,UAAAA,UAAU,SAAS,YAAY,GAAG;AACpC,UAAM,UAAU,UAAU,WAAW;AAErC,UAAM,mBACJ,YAAY,MAAM,eAAe,GAAG,YAAY,GAAG,QAAQ,KAAK,OAAO;AAEzE,WAAO,GAAG,gBAAgB,GAAG,UAAU,aAAa,EAAE,GAAG,UAAU,UAAU,GAAG;AAAA,EAClF,OAAO;AACL,WAAO,UAAU,aAAa;AAAA,EAChC;AACF;ACpCO,MAAM,aAAa,CAAC,UAAkB,MAAM,WAAW,OAAO,EAAE;;;;;;;;;;;"}
|
package/format.d.ts
CHANGED
|
@@ -34,6 +34,7 @@ interface NumberFormatSettings {
|
|
|
34
34
|
* Example: 0.010000000000000000000000000000000000000000000 -> 0.01
|
|
35
35
|
*/
|
|
36
36
|
cutZeros?: boolean;
|
|
37
|
+
cropDigitsOnly?: boolean;
|
|
37
38
|
}
|
|
38
39
|
declare const number: {
|
|
39
40
|
(rawValue: Maybe<string | number>, userSettings?: Maybe<NumberFormatSettings>): string;
|
package/format.js
CHANGED
|
@@ -23,7 +23,18 @@ const number = (rawValue, userSettings) => {
|
|
|
23
23
|
if (typeGuard.isNumber(value)) {
|
|
24
24
|
let raw = `${value}`;
|
|
25
25
|
if (digits !== false) {
|
|
26
|
-
|
|
26
|
+
if (settings.cropDigitsOnly) {
|
|
27
|
+
const [integerPart2, decimalPart2] = `${raw}`.split(".");
|
|
28
|
+
const leftPart = integerPart2;
|
|
29
|
+
const rightPart = (decimalPart2 || "").slice(0, digits).padEnd(digits, "0");
|
|
30
|
+
if (rightPart) {
|
|
31
|
+
raw = `${leftPart}.${rightPart}`;
|
|
32
|
+
} else {
|
|
33
|
+
raw = leftPart;
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
raw = value.toFixed(digits);
|
|
37
|
+
}
|
|
27
38
|
}
|
|
28
39
|
if (cutZeros) {
|
|
29
40
|
raw = `${+raw}`;
|
package/format.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.js","sources":["../src/format/constants.ts","../src/format/number.ts","../src/format/percent.ts","../src/format/skip-spaces.ts"],"sourcesContent":["export const NO_VALUE = '–'; // en-dash\n\nexport const HYPHEN = '-';\n\nexport const INFINITY = '∞';\n","import { parser } from 'yummies/parser';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\nimport { NO_VALUE } from './constants.js';\n\nexport interface NumberFormatSettings {\n delimiter?: string;\n /**\n * digitsOnlyForFloat - Show digits after decimal point only if they are not zeros after converting to number.\n * Example: \"0.00\" -> \"0\", \"0.10\" -> \"0.1\", but \"0.003\" -> \"0.003\"\n *\n * @default true\n */\n digitsOnlyForFloat?: boolean;\n /**\n * Text which will be returned if the value is undefined, null, NaN, Infinity or empty string.\n * Example: \"–\" will be returned if the value is undefined and emptyText is \"–\".\n */\n emptyText?: string;\n /**\n * Text to append to the end of the formatted number.\n * Example: if value is 1000 and postfix is \"₽\", result will be \"1 000₽\".\n */\n postfix?: string;\n /**\n * Fixed number of digits after the decimal point (number.toFixed() method)\n * If set to false, the truncation is ignored!\n */\n digits?: number | false;\n /**\n * Remove trailing zeros from the end of the number\n * Example: 0.010000000000000000000000000000000000000000000 -> 0.01\n */\n cutZeros?: boolean;\n}\n\nexport const number = (\n rawValue: Maybe<string | number>,\n userSettings?: Maybe<NumberFormatSettings>,\n): string => {\n const settings = {\n ...number.defaultSettings,\n ...userSettings,\n };\n\n const digits = settings.digits ?? 0;\n const cutZeros = settings?.cutZeros ?? false;\n const delimiter = settings.delimiter ?? ' ';\n const postfix = settings.postfix ?? '';\n const emptyText = settings.emptyText ?? NO_VALUE;\n const digitsOnlyForFloat = settings.digitsOnlyForFloat ?? true;\n\n let value: Maybe<number>;\n\n if (typeGuard.isString(rawValue)) {\n value = parser.number(value, { fallback: undefined });\n } else {\n value = rawValue;\n }\n\n if (typeGuard.isNumber(value)) {\n let raw: string = `${value}`;\n\n if (digits !== false) {\n raw = value.toFixed(digits);\n }\n if (cutZeros) {\n raw = `${+raw}`;\n }\n\n const [integerPart, decimalPart] = raw.split('.', 2);\n\n let formattedIntegerPart = integerPart;\n let formattedDecimalPart = '';\n\n if (decimalPart && (!digitsOnlyForFloat || !/^0+$/.test(decimalPart))) {\n formattedDecimalPart = `.${decimalPart}`;\n }\n\n const rgx = /(\\d+)(\\d{3})/;\n\n while (rgx.test(formattedIntegerPart) && delimiter) {\n formattedIntegerPart = formattedIntegerPart.replace(\n rgx,\n `$1${delimiter}$2`,\n );\n }\n\n return formattedIntegerPart + formattedDecimalPart + postfix;\n }\n\n return emptyText;\n};\n\nnumber.defaultSettings = {} as NumberFormatSettings;\n","import { parser } from 'yummies/parser';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\n\nimport { NO_VALUE } from './constants.js';\n\nexport interface PercentFormatSettings\n extends Omit<parser.NumberParserSettings, 'fallback'> {\n divider?: string;\n delimiter?: string;\n symbol?: string;\n emptyText?: string;\n}\n\n/**\n * 100 -> 100%\n * 99.123214412 -> 99.12%\n * 99.123214412 -> 99,12%\n */\nexport const percent = (\n value: Maybe<number | string>,\n settings?: PercentFormatSettings,\n) => {\n const numericValue = parser.number(value, {\n ...settings,\n digits: settings?.digits ?? 2,\n fallback: Number.NaN,\n });\n\n if (typeGuard.isNumber(numericValue)) {\n const divider = settings?.divider ?? '.';\n\n const formattedPercent =\n divider === '.' ? numericValue : `${numericValue}`.replace('.', divider);\n\n return `${formattedPercent}${settings?.delimiter ?? ''}${settings?.symbol ?? '%'}`;\n } else {\n return settings?.emptyText ?? NO_VALUE;\n }\n};\n","/**\n * Removes all spaces in string\n */\nexport const skipSpaces = (value: string) => value.replaceAll(/\\s/g, '');\n"],"names":[],"mappings":";;AAAO,MAAM,WAAW;AAEjB,MAAM,SAAS;AAEf,MAAM,WAAW;
|
|
1
|
+
{"version":3,"file":"format.js","sources":["../src/format/constants.ts","../src/format/number.ts","../src/format/percent.ts","../src/format/skip-spaces.ts"],"sourcesContent":["export const NO_VALUE = '–'; // en-dash\n\nexport const HYPHEN = '-';\n\nexport const INFINITY = '∞';\n","import { parser } from 'yummies/parser';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\nimport { NO_VALUE } from './constants.js';\n\nexport interface NumberFormatSettings {\n delimiter?: string;\n /**\n * digitsOnlyForFloat - Show digits after decimal point only if they are not zeros after converting to number.\n * Example: \"0.00\" -> \"0\", \"0.10\" -> \"0.1\", but \"0.003\" -> \"0.003\"\n *\n * @default true\n */\n digitsOnlyForFloat?: boolean;\n /**\n * Text which will be returned if the value is undefined, null, NaN, Infinity or empty string.\n * Example: \"–\" will be returned if the value is undefined and emptyText is \"–\".\n */\n emptyText?: string;\n /**\n * Text to append to the end of the formatted number.\n * Example: if value is 1000 and postfix is \"₽\", result will be \"1 000₽\".\n */\n postfix?: string;\n /**\n * Fixed number of digits after the decimal point (number.toFixed() method)\n * If set to false, the truncation is ignored!\n */\n digits?: number | false;\n /**\n * Remove trailing zeros from the end of the number\n * Example: 0.010000000000000000000000000000000000000000000 -> 0.01\n */\n cutZeros?: boolean;\n cropDigitsOnly?: boolean;\n}\n\nexport const number = (\n rawValue: Maybe<string | number>,\n userSettings?: Maybe<NumberFormatSettings>,\n): string => {\n const settings = {\n ...number.defaultSettings,\n ...userSettings,\n };\n\n const digits = settings.digits ?? 0;\n const cutZeros = settings?.cutZeros ?? false;\n const delimiter = settings.delimiter ?? ' ';\n const postfix = settings.postfix ?? '';\n const emptyText = settings.emptyText ?? NO_VALUE;\n const digitsOnlyForFloat = settings.digitsOnlyForFloat ?? true;\n\n let value: Maybe<number>;\n\n if (typeGuard.isString(rawValue)) {\n value = parser.number(value, { fallback: undefined });\n } else {\n value = rawValue;\n }\n\n if (typeGuard.isNumber(value)) {\n let raw: string = `${value}`;\n\n if (digits !== false) {\n if (settings.cropDigitsOnly) {\n const [integerPart, decimalPart] = `${raw}`.split('.');\n const leftPart = integerPart;\n const rightPart = (decimalPart || '')\n .slice(0, digits)\n .padEnd(digits, '0');\n\n if (rightPart) {\n raw = `${leftPart}.${rightPart}`;\n } else {\n raw = leftPart;\n }\n } else {\n raw = value.toFixed(digits);\n }\n }\n\n if (cutZeros) {\n raw = `${+raw}`;\n }\n\n const [integerPart, decimalPart] = raw.split('.', 2);\n\n let formattedIntegerPart = integerPart;\n let formattedDecimalPart = '';\n\n if (decimalPart && (!digitsOnlyForFloat || !/^0+$/.test(decimalPart))) {\n formattedDecimalPart = `.${decimalPart}`;\n }\n\n const rgx = /(\\d+)(\\d{3})/;\n\n while (rgx.test(formattedIntegerPart) && delimiter) {\n formattedIntegerPart = formattedIntegerPart.replace(\n rgx,\n `$1${delimiter}$2`,\n );\n }\n\n return formattedIntegerPart + formattedDecimalPart + postfix;\n }\n\n return emptyText;\n};\n\nnumber.defaultSettings = {} as NumberFormatSettings;\n","import { parser } from 'yummies/parser';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\n\nimport { NO_VALUE } from './constants.js';\n\nexport interface PercentFormatSettings\n extends Omit<parser.NumberParserSettings, 'fallback'> {\n divider?: string;\n delimiter?: string;\n symbol?: string;\n emptyText?: string;\n}\n\n/**\n * 100 -> 100%\n * 99.123214412 -> 99.12%\n * 99.123214412 -> 99,12%\n */\nexport const percent = (\n value: Maybe<number | string>,\n settings?: PercentFormatSettings,\n) => {\n const numericValue = parser.number(value, {\n ...settings,\n digits: settings?.digits ?? 2,\n fallback: Number.NaN,\n });\n\n if (typeGuard.isNumber(numericValue)) {\n const divider = settings?.divider ?? '.';\n\n const formattedPercent =\n divider === '.' ? numericValue : `${numericValue}`.replace('.', divider);\n\n return `${formattedPercent}${settings?.delimiter ?? ''}${settings?.symbol ?? '%'}`;\n } else {\n return settings?.emptyText ?? NO_VALUE;\n }\n};\n","/**\n * Removes all spaces in string\n */\nexport const skipSpaces = (value: string) => value.replaceAll(/\\s/g, '');\n"],"names":["integer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UAAM,MAAM;AAEZ,WAAO,IAAI,KAAK,oBAAoB,KAAK,WAAW;AAClD,6BAAuB,qBAAqB;AAAA,QAC1C;AAAA,QACA,KAAK,SAAS;AAAA,MAAA;AAAA,IAElB;AAEA,WAAO,uBAAuB,uBAAuB;AAAA,EACvD;AAEA,SAAO;AACT;AAEA,OAAO,kBAAkB,CAAA;AC3FlB,MAAM,UAAU,CACrB,OACA,aACG;AACH,QAAM,eAAe,OAAO,OAAO,OAAO;AAAA,IACxC,GAAG;AAAA,IACH,QAAQ,UAAU,UAAU;AAAA,IAC5B,UAAU,OAAO;AAAA,EAAA,CAClB;AAED,MAAI,UAAU,SAAS,YAAY,GAAG;AACpC,UAAM,UAAU,UAAU,WAAW;AAErC,UAAM,mBACJ,YAAY,MAAM,eAAe,GAAG,YAAY,GAAG,QAAQ,KAAK,OAAO;AAEzE,WAAO,GAAG,gBAAgB,GAAG,UAAU,aAAa,EAAE,GAAG,UAAU,UAAU,GAAG;AAAA,EAClF,OAAO;AACL,WAAO,UAAU,aAAa;AAAA,EAChC;AACF;ACpCO,MAAM,aAAa,CAAC,UAAkB,MAAM,WAAW,OAAO,EAAE;;;;;;;;;;"}
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -419,5 +419,10 @@ type AnyNumber = number & {};
|
|
|
419
419
|
* Helpful to use with union type literals (`true | AnyBoolean`)
|
|
420
420
|
*/
|
|
421
421
|
type AnyBoolean = boolean & {};
|
|
422
|
+
/**
|
|
423
|
+
* Upperfirst string type. It will capitalize the first letter of the string
|
|
424
|
+
* and leave the rest of the string unchanged.
|
|
425
|
+
*/
|
|
426
|
+
type UpperFirst<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S;
|
|
422
427
|
|
|
423
|
-
export type { AllPropertiesOptional, AnyBoolean, AnyFunction, AnyNumber, AnyObject, AnyPrimitive, AnyString, BrowserNativeObject, Class, CopyObject, DeepPartial, Defined, EmptyObject, ExtractEnumKeys, ExtractEnumValues, ExtractObjects, FalsyValues, HasKey, HasSpecificKey, IfEquals, IndexKeys, IsAny, IsArray, IsEmptyArray, IsFunction, IsObject, IsObjectEmpty, IsPartial, IsUnknown, KeyOfByValue, LiteralUnion, Maybe, MaybeFalsy, MaybeFn, MaybePromise, MaybeValues, NonReadonly, NonUndefined, Nullable, OmitByValue, OverrideKey, Params, PartialIf, PartialKeys, PickByValue, Primitive, ReadonlyKeys, RecordEntries, RenameKey, RequiredKeys, UnionToIntersection, Unpromise, ValueOf, WithRequired, WritableKeys };
|
|
428
|
+
export type { AllPropertiesOptional, AnyBoolean, AnyFunction, AnyNumber, AnyObject, AnyPrimitive, AnyString, BrowserNativeObject, Class, CopyObject, DeepPartial, Defined, EmptyObject, ExtractEnumKeys, ExtractEnumValues, ExtractObjects, FalsyValues, HasKey, HasSpecificKey, IfEquals, IndexKeys, IsAny, IsArray, IsEmptyArray, IsFunction, IsObject, IsObjectEmpty, IsPartial, IsUnknown, KeyOfByValue, LiteralUnion, Maybe, MaybeFalsy, MaybeFn, MaybePromise, MaybeValues, NonReadonly, NonUndefined, Nullable, OmitByValue, OverrideKey, Params, PartialIf, PartialKeys, PickByValue, Primitive, ReadonlyKeys, RecordEntries, RenameKey, RequiredKeys, UnionToIntersection, Unpromise, UpperFirst, ValueOf, WithRequired, WritableKeys };
|
package/types.global.d.ts
CHANGED
|
@@ -420,4 +420,9 @@ type AnyNumber = number & {};
|
|
|
420
420
|
* Helpful to use with union type literals (`true | AnyBoolean`)
|
|
421
421
|
*/
|
|
422
422
|
type AnyBoolean = boolean & {};
|
|
423
|
+
/**
|
|
424
|
+
* Upperfirst string type. It will capitalize the first letter of the string
|
|
425
|
+
* and leave the rest of the string unchanged.
|
|
426
|
+
*/
|
|
427
|
+
type UpperFirst<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S;
|
|
423
428
|
|