@augment-vir/common 18.0.0 → 18.1.1

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.
@@ -8,14 +8,21 @@ function addCommasToNumber(input) {
8
8
  if (typeof input === 'string' && isNaN(Number(input))) {
9
9
  return exports.NaNString;
10
10
  }
11
- const stringValue = String(input);
11
+ const numericValue = Number(input);
12
+ const isNegative = numericValue < 0;
13
+ const stringValue = String(Math.abs(numericValue));
12
14
  const [digits, decimalValues,] = stringValue.split('.');
13
15
  const decimalString = decimalValues ? `.${decimalValues}` : '';
14
16
  const separated = (0, regexp_1.safeMatch)(digits.split('').reverse().join(''), /.{1,3}/g)
15
17
  .reverse()
16
18
  .map((entry) => entry.split('').reverse().join(''));
17
- const withCommas = separated.join(',');
18
- return `${withCommas}${decimalString}`;
19
+ const valueWithCommas = separated.join(',');
20
+ const negativeMarker = isNegative ? '-' : '';
21
+ return [
22
+ negativeMarker,
23
+ valueWithCommas,
24
+ decimalString,
25
+ ].join('');
19
26
  }
20
27
  exports.addCommasToNumber = addCommasToNumber;
21
28
  function clamp(
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.removePrefix = exports.addPrefix = void 0;
4
+ function addPrefix({ value, prefix, }) {
5
+ if (String(value).startsWith(prefix)) {
6
+ return String(value);
7
+ }
8
+ else {
9
+ return `${prefix}${String(value)}`;
10
+ }
11
+ }
12
+ exports.addPrefix = addPrefix;
13
+ function removePrefix({ value, prefix, }) {
14
+ if (value.startsWith(prefix)) {
15
+ return value.substring(prefix.length);
16
+ }
17
+ else {
18
+ return value;
19
+ }
20
+ }
21
+ exports.removePrefix = removePrefix;
@@ -2,8 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.urlToSearchParamsObject = exports.objectToSearchParamsString = void 0;
4
4
  const boolean_1 = require("../boolean");
5
+ const common_string_1 = require("../common-string");
5
6
  const matches_object_shape_1 = require("../object/matches-object-shape");
6
7
  const runtime_type_of_1 = require("../runtime-type-of");
8
+ const prefixes_1 = require("./prefixes");
7
9
  function objectToSearchParamsString(inputObject) {
8
10
  const valueStrings = Object.entries(inputObject)
9
11
  .map(([key, value,]) => {
@@ -21,9 +23,26 @@ function objectToSearchParamsString(inputObject) {
21
23
  }
22
24
  }
23
25
  exports.objectToSearchParamsString = objectToSearchParamsString;
26
+ function splitSearchString(searchString) {
27
+ const params = (0, prefixes_1.removePrefix)({ value: searchString, prefix: '?' }).split('&');
28
+ const paramEntries = params
29
+ .map((param) => {
30
+ const [key, ...everythingElse] = (0, common_string_1.typedSplit)(param, '=');
31
+ const value = everythingElse.join('');
32
+ if (!value && !key) {
33
+ return undefined;
34
+ }
35
+ return [
36
+ key,
37
+ value,
38
+ ];
39
+ })
40
+ .filter(boolean_1.isTruthy);
41
+ return paramEntries;
42
+ }
24
43
  function urlToSearchParamsObject(inputUrl, verifyShape) {
25
- const urlForSearchParams = (0, runtime_type_of_1.isRuntimeTypeOf)(inputUrl, 'string') ? new URL(inputUrl) : inputUrl;
26
- const searchEntries = Array.from(urlForSearchParams.searchParams.entries());
44
+ const ensuredUrl = (0, runtime_type_of_1.isRuntimeTypeOf)(inputUrl, 'string') ? new URL(inputUrl) : inputUrl;
45
+ const searchEntries = splitSearchString(ensuredUrl.search);
27
46
  const paramsObject = Object.fromEntries(searchEntries);
28
47
  if (verifyShape) {
29
48
  (0, matches_object_shape_1.assertMatchesObjectShape)(paramsObject, verifyShape);
@@ -5,36 +5,36 @@ const number_1 = require("../number");
5
5
  exports.percentSuffix = '%';
6
6
  exports.pxSuffix = 'px';
7
7
  function addPx(input) {
8
- return addSuffix(input, exports.pxSuffix);
8
+ return addSuffix({ value: input, suffix: exports.pxSuffix });
9
9
  }
10
10
  exports.addPx = addPx;
11
11
  function removePx(input) {
12
- return (0, number_1.toEnsuredNumber)(input.replace(/px$/, ''));
12
+ return (0, number_1.toEnsuredNumber)(removeSuffix({ value: input, suffix: exports.pxSuffix }));
13
13
  }
14
14
  exports.removePx = removePx;
15
15
  function addPercent(input) {
16
- return addSuffix(input, exports.percentSuffix);
16
+ return addSuffix({ value: input, suffix: exports.percentSuffix });
17
17
  }
18
18
  exports.addPercent = addPercent;
19
19
  function removePercent(input) {
20
- return (0, number_1.toEnsuredNumber)(removeSuffix(input, exports.percentSuffix));
20
+ return (0, number_1.toEnsuredNumber)(removeSuffix({ value: input, suffix: exports.percentSuffix }));
21
21
  }
22
22
  exports.removePercent = removePercent;
23
- function addSuffix(input, suffix) {
24
- if (String(input).endsWith(suffix)) {
25
- return String(input);
23
+ function addSuffix({ value, suffix, }) {
24
+ if (String(value).endsWith(suffix)) {
25
+ return String(value);
26
26
  }
27
27
  else {
28
- return `${String(input)}${suffix}`;
28
+ return `${String(value)}${suffix}`;
29
29
  }
30
30
  }
31
31
  exports.addSuffix = addSuffix;
32
- function removeSuffix(input, suffix) {
33
- if (input.endsWith(suffix)) {
34
- return input.substring(0, input.length - suffix.length);
32
+ function removeSuffix({ value, suffix, }) {
33
+ if (value.endsWith(suffix)) {
34
+ return value.substring(0, value.length - suffix.length);
35
35
  }
36
36
  else {
37
- return input;
37
+ return value;
38
38
  }
39
39
  }
40
40
  exports.removeSuffix = removeSuffix;
package/dist/cjs/index.js CHANGED
@@ -41,6 +41,7 @@ __exportStar(require("./augments/object/typed-has-property"), exports);
41
41
  __exportStar(require("./augments/promise"), exports);
42
42
  __exportStar(require("./augments/regexp"), exports);
43
43
  __exportStar(require("./augments/runtime-type-of"), exports);
44
+ __exportStar(require("./augments/string/prefixes"), exports);
44
45
  __exportStar(require("./augments/string/search-params"), exports);
45
46
  __exportStar(require("./augments/string/suffixes"), exports);
46
47
  __exportStar(require("./augments/string/url"), exports);
@@ -5,14 +5,21 @@ export function addCommasToNumber(input) {
5
5
  if (typeof input === 'string' && isNaN(Number(input))) {
6
6
  return NaNString;
7
7
  }
8
- const stringValue = String(input);
8
+ const numericValue = Number(input);
9
+ const isNegative = numericValue < 0;
10
+ const stringValue = String(Math.abs(numericValue));
9
11
  const [digits, decimalValues,] = stringValue.split('.');
10
12
  const decimalString = decimalValues ? `.${decimalValues}` : '';
11
13
  const separated = safeMatch(digits.split('').reverse().join(''), /.{1,3}/g)
12
14
  .reverse()
13
15
  .map((entry) => entry.split('').reverse().join(''));
14
- const withCommas = separated.join(',');
15
- return `${withCommas}${decimalString}`;
16
+ const valueWithCommas = separated.join(',');
17
+ const negativeMarker = isNegative ? '-' : '';
18
+ return [
19
+ negativeMarker,
20
+ valueWithCommas,
21
+ decimalString,
22
+ ].join('');
16
23
  }
17
24
  export function clamp(
18
25
  /**
@@ -0,0 +1,16 @@
1
+ export function addPrefix({ value, prefix, }) {
2
+ if (String(value).startsWith(prefix)) {
3
+ return String(value);
4
+ }
5
+ else {
6
+ return `${prefix}${String(value)}`;
7
+ }
8
+ }
9
+ export function removePrefix({ value, prefix, }) {
10
+ if (value.startsWith(prefix)) {
11
+ return value.substring(prefix.length);
12
+ }
13
+ else {
14
+ return value;
15
+ }
16
+ }
@@ -1,6 +1,8 @@
1
1
  import { isTruthy } from '../boolean';
2
+ import { typedSplit } from '../common-string';
2
3
  import { assertMatchesObjectShape } from '../object/matches-object-shape';
3
4
  import { isRuntimeTypeOf } from '../runtime-type-of';
5
+ import { removePrefix } from './prefixes';
4
6
  export function objectToSearchParamsString(inputObject) {
5
7
  const valueStrings = Object.entries(inputObject)
6
8
  .map(([key, value,]) => {
@@ -17,9 +19,26 @@ export function objectToSearchParamsString(inputObject) {
17
19
  return '';
18
20
  }
19
21
  }
22
+ function splitSearchString(searchString) {
23
+ const params = removePrefix({ value: searchString, prefix: '?' }).split('&');
24
+ const paramEntries = params
25
+ .map((param) => {
26
+ const [key, ...everythingElse] = typedSplit(param, '=');
27
+ const value = everythingElse.join('');
28
+ if (!value && !key) {
29
+ return undefined;
30
+ }
31
+ return [
32
+ key,
33
+ value,
34
+ ];
35
+ })
36
+ .filter(isTruthy);
37
+ return paramEntries;
38
+ }
20
39
  export function urlToSearchParamsObject(inputUrl, verifyShape) {
21
- const urlForSearchParams = isRuntimeTypeOf(inputUrl, 'string') ? new URL(inputUrl) : inputUrl;
22
- const searchEntries = Array.from(urlForSearchParams.searchParams.entries());
40
+ const ensuredUrl = isRuntimeTypeOf(inputUrl, 'string') ? new URL(inputUrl) : inputUrl;
41
+ const searchEntries = splitSearchString(ensuredUrl.search);
23
42
  const paramsObject = Object.fromEntries(searchEntries);
24
43
  if (verifyShape) {
25
44
  assertMatchesObjectShape(paramsObject, verifyShape);
@@ -2,30 +2,30 @@ import { toEnsuredNumber } from '../number';
2
2
  export const percentSuffix = '%';
3
3
  export const pxSuffix = 'px';
4
4
  export function addPx(input) {
5
- return addSuffix(input, pxSuffix);
5
+ return addSuffix({ value: input, suffix: pxSuffix });
6
6
  }
7
7
  export function removePx(input) {
8
- return toEnsuredNumber(input.replace(/px$/, ''));
8
+ return toEnsuredNumber(removeSuffix({ value: input, suffix: pxSuffix }));
9
9
  }
10
10
  export function addPercent(input) {
11
- return addSuffix(input, percentSuffix);
11
+ return addSuffix({ value: input, suffix: percentSuffix });
12
12
  }
13
13
  export function removePercent(input) {
14
- return toEnsuredNumber(removeSuffix(input, percentSuffix));
14
+ return toEnsuredNumber(removeSuffix({ value: input, suffix: percentSuffix }));
15
15
  }
16
- export function addSuffix(input, suffix) {
17
- if (String(input).endsWith(suffix)) {
18
- return String(input);
16
+ export function addSuffix({ value, suffix, }) {
17
+ if (String(value).endsWith(suffix)) {
18
+ return String(value);
19
19
  }
20
20
  else {
21
- return `${String(input)}${suffix}`;
21
+ return `${String(value)}${suffix}`;
22
22
  }
23
23
  }
24
- export function removeSuffix(input, suffix) {
25
- if (input.endsWith(suffix)) {
26
- return input.substring(0, input.length - suffix.length);
24
+ export function removeSuffix({ value, suffix, }) {
25
+ if (value.endsWith(suffix)) {
26
+ return value.substring(0, value.length - suffix.length);
27
27
  }
28
28
  else {
29
- return input;
29
+ return value;
30
30
  }
31
31
  }
package/dist/esm/index.js CHANGED
@@ -25,6 +25,7 @@ export * from './augments/object/typed-has-property';
25
25
  export * from './augments/promise';
26
26
  export * from './augments/regexp';
27
27
  export * from './augments/runtime-type-of';
28
+ export * from './augments/string/prefixes';
28
29
  export * from './augments/string/search-params';
29
30
  export * from './augments/string/suffixes';
30
31
  export * from './augments/string/url';
@@ -0,0 +1,9 @@
1
+ export type WithPrefix<Prefix extends string> = `${Prefix}${string}`;
2
+ export declare function addPrefix<const Prefix extends string>({ value, prefix, }: {
3
+ value: unknown;
4
+ prefix: Prefix;
5
+ }): WithPrefix<Prefix>;
6
+ export declare function removePrefix<const Prefix extends string>({ value, prefix, }: {
7
+ value: WithPrefix<Prefix> | string;
8
+ prefix: Prefix;
9
+ }): string;
@@ -1,5 +1,5 @@
1
1
  import { Primitive } from 'type-fest';
2
2
  export type SearchParamObjectBase = Record<string, Exclude<Primitive, symbol>>;
3
3
  export declare function objectToSearchParamsString(inputObject: SearchParamObjectBase): string;
4
- export declare function urlToSearchParamsObject<VerifyShapeGeneric extends SearchParamObjectBase>(inputUrl: string | Pick<URL, 'searchParams'>, verifyShape: VerifyShapeGeneric): VerifyShapeGeneric;
5
- export declare function urlToSearchParamsObject<VerifyShapeGeneric extends SearchParamObjectBase>(inputUrl: string | Pick<URL, 'searchParams'>, verifyShape?: VerifyShapeGeneric | undefined): Record<string, string>;
4
+ export declare function urlToSearchParamsObject<VerifyShapeGeneric extends SearchParamObjectBase>(inputUrl: string | Pick<URL, 'search'>, verifyShape: VerifyShapeGeneric): VerifyShapeGeneric;
5
+ export declare function urlToSearchParamsObject<VerifyShapeGeneric extends SearchParamObjectBase>(inputUrl: string | Pick<URL, 'search'>, verifyShape?: VerifyShapeGeneric | undefined): Record<string, string>;
@@ -7,5 +7,11 @@ export declare function addPx(input: number | string): WithPx;
7
7
  export declare function removePx(input: string): number;
8
8
  export declare function addPercent(input: number | string): WithPercent;
9
9
  export declare function removePercent(input: string): number;
10
- export declare function addSuffix<const Suffix extends string>(input: unknown, suffix: Suffix): WithSuffix<Suffix>;
11
- export declare function removeSuffix<const Suffix extends string>(input: WithSuffix<Suffix> | string, suffix: Suffix): string;
10
+ export declare function addSuffix<const Suffix extends string>({ value, suffix, }: {
11
+ value: unknown;
12
+ suffix: Suffix;
13
+ }): WithSuffix<Suffix>;
14
+ export declare function removeSuffix<const Suffix extends string>({ value, suffix, }: {
15
+ value: WithSuffix<Suffix> | string;
16
+ suffix: Suffix;
17
+ }): string;
@@ -25,6 +25,7 @@ export * from './augments/object/typed-has-property';
25
25
  export * from './augments/promise';
26
26
  export * from './augments/regexp';
27
27
  export * from './augments/runtime-type-of';
28
+ export * from './augments/string/prefixes';
28
29
  export * from './augments/string/search-params';
29
30
  export * from './augments/string/suffixes';
30
31
  export * from './augments/string/url';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "18.0.0",
3
+ "version": "18.1.1",
4
4
  "homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
5
5
  "bugs": {
6
6
  "url": "https://github.com/electrovir/augment-vir/issues"