@tempots/std 0.9.6 → 0.9.7

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.
Files changed (128) hide show
  1. package/arrays.d.ts +49 -0
  2. package/arrays.js +249 -0
  3. package/async-result.d.ts +37 -0
  4. package/async-result.js +75 -0
  5. package/bigint.d.ts +18 -0
  6. package/bigint.js +110 -0
  7. package/booleans.d.ts +23 -0
  8. package/{src/booleans.ts → booleans.js} +33 -38
  9. package/colors/cmyk.d.ts +21 -0
  10. package/colors/cmyk.js +54 -0
  11. package/colors/convert.d.ts +283 -0
  12. package/colors/convert.js +742 -0
  13. package/colors/hsl.d.ts +24 -0
  14. package/colors/hsl.js +56 -0
  15. package/colors/hsla.d.ts +18 -0
  16. package/colors/hsla.js +35 -0
  17. package/colors/hsluv.d.ts +24 -0
  18. package/colors/hsluv.js +56 -0
  19. package/colors/hsv.d.ts +24 -0
  20. package/colors/hsv.js +54 -0
  21. package/colors/lab.d.ts +24 -0
  22. package/colors/lab.js +54 -0
  23. package/colors/lch.d.ts +19 -0
  24. package/colors/lch.js +44 -0
  25. package/colors/luv.d.ts +19 -0
  26. package/colors/luv.js +45 -0
  27. package/colors/rgb.d.ts +13 -0
  28. package/colors/rgb.js +47 -0
  29. package/colors/rgba.d.ts +12 -0
  30. package/colors/rgba.js +44 -0
  31. package/colors/srgb.d.ts +24 -0
  32. package/colors/srgb.js +51 -0
  33. package/colors/xyz.d.ts +19 -0
  34. package/colors/xyz.js +41 -0
  35. package/edit.d.ts +20 -0
  36. package/edit.js +29 -0
  37. package/equals.d.ts +3 -0
  38. package/equals.js +122 -0
  39. package/functions.d.ts +20 -0
  40. package/functions.js +38 -0
  41. package/json.d.ts +14 -0
  42. package/json.js +33 -0
  43. package/match.d.ts +16 -0
  44. package/match.js +45 -0
  45. package/maybe.d.ts +9 -0
  46. package/{src/maybe.ts → maybe.js} +11 -18
  47. package/memoize.d.ts +1 -0
  48. package/memoize.js +9 -0
  49. package/newtype.d.ts +28 -0
  50. package/newtype.js +29 -0
  51. package/numbers.d.ts +104 -0
  52. package/numbers.js +183 -0
  53. package/objects.d.ts +9 -0
  54. package/objects.js +33 -0
  55. package/ord.d.ts +19 -0
  56. package/ord.js +73 -0
  57. package/package.json +2 -2
  58. package/reg-exps.d.ts +10 -0
  59. package/{src/reg-exps.ts → reg-exps.js} +19 -24
  60. package/result.d.ts +31 -0
  61. package/result.js +95 -0
  62. package/strings.d.ts +314 -0
  63. package/strings.js +685 -0
  64. package/types/assert.d.ts +12 -0
  65. package/types/assert.js +13 -0
  66. package/types/differentiate.d.ts +13 -0
  67. package/types/differentiate.js +14 -0
  68. package/types/functions.d.ts +22 -0
  69. package/types/functions.js +13 -0
  70. package/types/generic.d.ts +9 -0
  71. package/types/generic.js +13 -0
  72. package/types/objects.d.ts +50 -0
  73. package/types/objects.js +13 -0
  74. package/types/tuples.d.ts +44 -0
  75. package/types/tuples.js +24 -0
  76. package/{src/types/utility.ts → types/utility.d.ts} +2 -3
  77. package/types/utility.js +1 -0
  78. package/uuid.d.ts +13 -0
  79. package/uuid.js +56 -0
  80. package/validation.d.ts +23 -0
  81. package/validation.js +44 -0
  82. package/src/arrays.ts +0 -296
  83. package/src/async-result.ts +0 -103
  84. package/src/bigint.ts +0 -111
  85. package/src/colors/cmyk.ts +0 -84
  86. package/src/colors/convert.ts +0 -1093
  87. package/src/colors/hsl.ts +0 -73
  88. package/src/colors/hsla.ts +0 -45
  89. package/src/colors/hsluv.ts +0 -73
  90. package/src/colors/hsv.ts +0 -75
  91. package/src/colors/lab.ts +0 -69
  92. package/src/colors/lch.ts +0 -53
  93. package/src/colors/luv.ts +0 -56
  94. package/src/colors/rgb.ts +0 -55
  95. package/src/colors/rgba.ts +0 -53
  96. package/src/colors/srgb.ts +0 -72
  97. package/src/colors/xyz.ts +0 -52
  98. package/src/edit.ts +0 -29
  99. package/src/equals.ts +0 -116
  100. package/src/functions.ts +0 -108
  101. package/src/json.ts +0 -52
  102. package/src/match.ts +0 -88
  103. package/src/memoize.ts +0 -9
  104. package/src/newtype.ts +0 -59
  105. package/src/numbers.ts +0 -222
  106. package/src/objects.ts +0 -47
  107. package/src/ord.ts +0 -79
  108. package/src/result.ts +0 -140
  109. package/src/strings.ts +0 -768
  110. package/src/types/assert.ts +0 -96
  111. package/src/types/differentiate.ts +0 -89
  112. package/src/types/functions.ts +0 -114
  113. package/src/types/generic.ts +0 -42
  114. package/src/types/objects.ts +0 -212
  115. package/src/types/tuples.ts +0 -244
  116. package/src/uuid.ts +0 -61
  117. package/src/validation.ts +0 -69
  118. package/test/arrays.spec.ts +0 -410
  119. package/test/colors.spec.ts +0 -406
  120. package/test/commmon.ts +0 -9
  121. package/test/equals.spec.ts +0 -165
  122. package/test/functions.spec.ts +0 -9
  123. package/test/index.d.ts +0 -20
  124. package/test/objects.spec.ts +0 -22
  125. package/test/reg-exps.spec.ts +0 -33
  126. package/test/strings.spec.ts +0 -333
  127. package/test/uuid.spec.ts +0 -35
  128. package/tsconfig.json +0 -19
package/src/edit.ts DELETED
@@ -1,29 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
- export abstract class Edit<V, I, E> {
3
- abstract readonly type: 'Input' | 'Error' | 'Value'
4
- }
5
-
6
- export class Input<I> extends Edit<never, I, never> {
7
- override readonly type = 'Input' as const
8
- constructor (readonly input: I) {
9
- super()
10
- }
11
- }
12
-
13
- export class Error<I, E> extends Edit<never, I, E> {
14
- override readonly type = 'Error' as const
15
- constructor (readonly input: I, readonly error: E) {
16
- super()
17
- }
18
- }
19
-
20
- export class Value<V> extends Edit<V, never, never> {
21
- override readonly type = 'Value' as const
22
- constructor (readonly value: V) {
23
- super()
24
- }
25
- }
26
-
27
- export type Validate<V, I, E> = (
28
- input: Input<I> | Error<I, E>
29
- ) => Error<I, E> | Value<V>
package/src/equals.ts DELETED
@@ -1,116 +0,0 @@
1
- /*
2
- Copyright 2019 Google LLC
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
- https://www.apache.org/licenses/LICENSE-2.0
7
- Unless required by applicable law or agreed to in writing, software
8
- distributed under the License is distributed on an "AS IS" BASIS,
9
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
- See the License for the specific language governing permissions and
11
- limitations under the License.
12
- */
13
-
14
- export function strictEqual<A> (a: A, b: A): boolean {
15
- // eslint-disable-next-line no-self-compare
16
- return a === b || (a !== a && b !== b)
17
- }
18
-
19
- export function deepEqual<A> (a: A, b: A): boolean {
20
- if (strictEqual(a, b)) return true
21
- if (a == null || b == null) return false
22
- const aIsArr = Array.isArray(a)
23
- const bIsArr = Array.isArray(b)
24
-
25
- if (aIsArr !== bIsArr) return false
26
- if (aIsArr) {
27
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
- const aArr: never[] = a as any
29
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
- const bArr: never[] = b as any
31
- const aLength = aArr.length
32
- if (aLength !== bArr.length) return false
33
- for (let i = 0; i < aLength; i++) {
34
- if (!deepEqual(aArr[i], bArr[i])) return false
35
- }
36
- return true
37
- }
38
-
39
- const aIsDate = a instanceof Date
40
- const bIsDate = b instanceof Date
41
- if (aIsDate !== bIsDate) return false
42
- if (aIsDate) {
43
- const aDate: Date = a as never
44
- const bDate: Date = b as never
45
- return +aDate === +bDate
46
- }
47
-
48
- const aIsSet = a instanceof Set
49
- const bIsSet = b instanceof Set
50
- if (aIsSet !== bIsSet) return false
51
- if (aIsSet) {
52
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
- const aSet: Set<any> = a as never
54
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
- const bSet: Set<any> = b as never
56
- if (aSet.size !== bSet.size) return false
57
- const it = aSet.keys()
58
- // eslint-disable-next-line no-constant-condition
59
- while (true) {
60
- const curr = it.next()
61
- if (curr.done ?? false) break
62
- if (!bSet.has(curr.value)) return false
63
- }
64
- return true
65
- }
66
-
67
- const aIsMap = a instanceof Map
68
- const bIsMap = b instanceof Map
69
- if (aIsMap !== bIsMap) return false
70
- if (aIsMap) {
71
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
- const aMap: Map<any, any> = a as never
73
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
- const bMap: Map<any, any> = b as never
75
- const aMapLength = aMap.size
76
- if (aMapLength !== bMap.size) return false
77
- const it = aMap.keys()
78
- // eslint-disable-next-line no-constant-condition
79
- while (true) {
80
- const curr = it.next()
81
- if (curr.done ?? false) break
82
- if (!deepEqual(aMap.get(curr.value), bMap.get(curr.value))) return false
83
- }
84
- return true
85
- }
86
-
87
- const aIsObj = typeof a === 'object'
88
- const bIsObj = typeof b === 'object'
89
- if (aIsObj !== bIsObj) return false
90
- if (aIsObj) {
91
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
92
- const aObj: any = a
93
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
94
- const bObj: any = b
95
- const aFields = Object.keys(aObj)
96
- const bFields = Object.keys(bObj)
97
- const aLength = aFields.length
98
-
99
- if (aLength !== bFields.length) return false
100
-
101
- for (let i = 0; i < aLength; i++) {
102
- const field = aFields[i]
103
- if (!Object.prototype.hasOwnProperty.call(bObj, field)) return false
104
- if (!deepEqual(aObj[field], bObj[field])) return false
105
- }
106
-
107
- return true
108
- }
109
-
110
- return false
111
- }
112
-
113
- export function looseEqual<T> (a: T, b: T): boolean {
114
- // eslint-disable-next-line eqeqeq
115
- return a == b
116
- }
package/src/functions.ts DELETED
@@ -1,108 +0,0 @@
1
- /*
2
- Copyright 2019 Google LLC
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
- https://www.apache.org/licenses/LICENSE-2.0
7
- Unless required by applicable law or agreed to in writing, software
8
- distributed under the License is distributed on an "AS IS" BASIS,
9
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
- See the License for the specific language governing permissions and
11
- limitations under the License.
12
- */
13
-
14
- import { type Fun1, type FirstArgument } from './types/functions'
15
-
16
- export function compose<A> (): (a: A) => A
17
- export function compose<A, B> (f1: (a: A) => B): (a: A) => B
18
- export function compose<A, B, C> (f1: (a: A) => B, f2: (b: B) => C): (a: A) => C
19
- export function compose<A, B, C, D> (
20
- f1: (a: A) => B,
21
- f2: (b: B) => C,
22
- f3: (c: C) => D
23
- ): (a: A) => D
24
- export function compose<A, B, C, D, E> (
25
- f1: (a: A) => B,
26
- f2: (b: B) => C,
27
- f3: (c: C) => D,
28
- f4: (d: D) => E
29
- ): (a: A) => E
30
- export function compose<A, B, C, D, E, F> (
31
- f1: (a: A) => B,
32
- f2: (b: B) => C,
33
- f3: (c: C) => D,
34
- f4: (d: D) => E,
35
- f5: (e: E) => F
36
- ): (a: A) => F
37
- export function compose<A, B, C, D, E, F, G> (
38
- f1: (a: A) => B,
39
- f2: (b: B) => C,
40
- f3: (c: C) => D,
41
- f4: (d: D) => E,
42
- f5: (e: E) => F,
43
- f6: (f: F) => G
44
- ): (a: A) => G
45
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
- export function compose<Args extends Array<Fun1<any, any>>> (...args: Args) {
47
- return (a: FirstArgument<Args[0]>) => args.reduce((acc, f) => f(acc), a)
48
- }
49
-
50
- export function identity<T> (v: T): T {
51
- return v
52
- }
53
-
54
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
- export function curryLeft<A, Rest extends any[], Ret> (
56
- f: (a: A, ...rest: Rest) => Ret
57
- ) {
58
- return (a: A) =>
59
- (...rest: Rest): Ret =>
60
- f(a, ...rest)
61
- }
62
-
63
- export function curryRight<A, B, C, D> (
64
- f: (a: A, b: B, c: C) => D
65
- ): (a: A, b: B) => (c: C) => D
66
- export function curryRight<A, B, C, D, E> (
67
- f: (a: A, b: B, c: C, d: D) => E
68
- ): (a: A, b: B, c: C) => (d: D) => E
69
- export function curryRight<A, B, C, D, E, F> (
70
- f: (a: A, b: B, c: C, d: D, e: E) => F
71
- ): (a: A, b: B, c: C, d: D) => (e: E) => F
72
- export function curryRight<A, B, C, D, E, F, G> (
73
- f: (a: A, b: B, c: C, d: D, e: E, f: F) => G
74
- ): (a: A, b: B, c: C, d: D, e: E) => (f: F) => G
75
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
- export function curryRight<Ret> (f: (...rest: any[]) => Ret) {
77
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
78
- return (...args: any[]) =>
79
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
80
- (a: any): Ret =>
81
- f(...args.concat([a]))
82
- }
83
-
84
- export function flip<A, B, C> (f: (a: A, b: B) => C): (b: B, a: A) => C
85
- export function flip<A, B, C, D> (
86
- f: (a: A, b: B, c: C) => D
87
- ): (c: C, b: B, a: A) => D
88
- export function flip<A, B, C, D, E> (
89
- f: (a: A, b: B, c: C, d: D) => E
90
- ): (d: D, c: C, b: B, a: A) => E
91
- export function flip<A, B, C, D, E> (
92
- f: (a: A, b: B, c: C, d: D) => E
93
- ): (d: D, c: C, b: B, a: A) => E
94
- export function flip<A, B, C, D, E, F> (
95
- f: (a: A, b: B, c: C, d: D, e: E) => F
96
- ): (e: E, d: D, c: C, b: B, a: A) => F
97
- export function flip<A, B, C, D, E, F, G> (
98
- f: (a: A, b: B, c: C, d: D, e: E, f: F) => G
99
- ): (f: F, e: E, d: D, c: C, b: B, a: A) => G
100
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
101
- export function flip<Ret> (f: (...rest: any[]) => Ret) {
102
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
- return (...args: any[]) => f(...args.reverse())
104
- }
105
-
106
- export function id<T> (v: T): T {
107
- return v
108
- }
package/src/json.ts DELETED
@@ -1,52 +0,0 @@
1
- /*
2
- Copyright 2019 Google LLC
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
- https://www.apache.org/licenses/LICENSE-2.0
7
- Unless required by applicable law or agreed to in writing, software
8
- distributed under the License is distributed on an "AS IS" BASIS,
9
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
- See the License for the specific language governing permissions and
11
- limitations under the License.
12
- */
13
-
14
- import { Result } from './result'
15
-
16
- export type JSONPrimitive = string | boolean | number | null
17
- // eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
18
- export interface JSONObject {
19
- [k: string]: JSONValue
20
- }
21
- export type JSONArray = JSONValue[]
22
- export type JSONValue = JSONPrimitive | JSONObject | JSONArray
23
-
24
- export function deserialize (s: string): Result<JSONValue, string> {
25
- try {
26
- return Result.success(JSON.parse(s))
27
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
- } catch (e: any) {
29
- return Result.failure(e.message)
30
- }
31
- }
32
-
33
- export function makeSerializer (options: {
34
- minified?: boolean
35
- whitelist?: string[]
36
- }) {
37
- return function serialize (v: JSONValue): string {
38
- return JSON.stringify(
39
- v,
40
- options.whitelist,
41
- options.minified === false ? undefined : 2
42
- )
43
- }
44
- }
45
-
46
- export function serialize (v: JSONValue): string {
47
- return JSON.stringify(v, null, 2)
48
- }
49
-
50
- export function serializeMinified (v: JSONValue): string {
51
- return JSON.stringify(v)
52
- }
package/src/match.ts DELETED
@@ -1,88 +0,0 @@
1
- /*
2
- Copyright 2019 Google LLC
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
- https://www.apache.org/licenses/LICENSE-2.0
7
- Unless required by applicable law or agreed to in writing, software
8
- distributed under the License is distributed on an "AS IS" BASIS,
9
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
- See the License for the specific language governing permissions and
11
- limitations under the License.
12
- */
13
-
14
- import { type Differentiate, type DifferentiateAt } from './types/differentiate'
15
- import { type AnyKey } from './types/utility'
16
- import { type ObjectWithField, type ObjectWithPath, type TypeAtPath } from './types/objects'
17
-
18
- export function matchLiteral<A extends AnyKey, B> (
19
- input: A,
20
- matcher: { [k in A]: B }
21
- ): B {
22
- return matcher[input]
23
- }
24
-
25
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
- export function match<F extends AnyKey, T extends ObjectWithField<F, any>, B> (
27
- input: T,
28
- field: F,
29
- matcher: { [k in T[F]]: (arg: Differentiate<F, T, k>) => B }
30
- ): B {
31
- const k = input[field]
32
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
- return matcher[k](input as any)
34
- }
35
-
36
- export function deepMatch<
37
- Path extends AnyKey[],
38
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
- T extends ObjectWithPath<Path, any>,
40
- B
41
- > (
42
- input: T,
43
- path: Path,
44
- matcher: {
45
- [k in TypeAtPath<Path, T>]: (arg: DifferentiateAt<Path, T, k>) => B
46
- }
47
- ): B {
48
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
49
- const k = path.reduce((res: any, key) => res[key], input) as TypeAtPath<
50
- Path,
51
- T
52
- >
53
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
- return matcher[k](input as any)
55
- }
56
-
57
- export function createMatch<F extends AnyKey> (field: F) {
58
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
- return <T extends ObjectWithField<F, any>, B>(
60
- input: T,
61
- matcher: { [k in T[F]]: (arg: Differentiate<F, T, k>) => B }
62
- ): B => {
63
- const k = input[field]
64
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
65
- return matcher[k](input as any)
66
- }
67
- }
68
-
69
- export const createDeepMatch =
70
- <Path extends AnyKey[]>(...path: Path) =>
71
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
- <T extends ObjectWithPath<Path, any>, B>(
73
- input: T,
74
- matcher: {
75
- [k in TypeAtPath<Path, T>]: (arg: DifferentiateAt<Path, T, k>) => B
76
- }
77
- ): B => {
78
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
79
- const k = path.reduce((res: any, key) => res[key], input) as TypeAtPath<
80
- Path,
81
- T
82
- >
83
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
84
- return matcher[k](input as any)
85
- }
86
-
87
- export const matchKind = createMatch('kind')
88
- export const matchType = createMatch('type')
package/src/memoize.ts DELETED
@@ -1,9 +0,0 @@
1
- export function memoize<T> (f: () => T): () => T {
2
- let value: T | undefined
3
- return () => {
4
- if (value === undefined) {
5
- value = f()
6
- }
7
- return value
8
- }
9
- }
package/src/newtype.ts DELETED
@@ -1,59 +0,0 @@
1
- /*
2
- Copyright 2019 Google LLC
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
- https://www.apache.org/licenses/LICENSE-2.0
7
- Unless required by applicable law or agreed to in writing, software
8
- distributed under the License is distributed on an "AS IS" BASIS,
9
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
- See the License for the specific language governing permissions and
11
- limitations under the License.
12
- */
13
-
14
- /**
15
- * Usage:
16
- *
17
- * ```ts
18
- * export interface Int extends Newtype<
19
- * number,
20
- * { readonly Int: unique symbol }
21
- * > {}
22
- *
23
- * export const Int = new class extends NewtypeClass<Int> {
24
- * isValid(v: number) { return Number.isInteger(v) }
25
- * }()
26
- * ```
27
- */
28
-
29
- import { type Maybe } from './maybe'
30
-
31
- export type TypeOfN<T extends Newtype<unknown, unknown>> = T['_T']
32
-
33
- export interface Newtype<V, S> {
34
- readonly _T: V
35
- readonly _S: S
36
- }
37
-
38
- export abstract class NewtypeClass<T extends Newtype<unknown, unknown>> {
39
- abstract isValid (v: TypeOfN<T>): boolean
40
- unsafeOf (v: TypeOfN<T>): T {
41
- return v as unknown as T
42
- }
43
-
44
- of (v: TypeOfN<T>): Maybe<T> {
45
- return this.isValid(v) ? (v as unknown as T) : undefined
46
- }
47
-
48
- get (v: T): TypeOfN<T> {
49
- return v as unknown as TypeOfN<T>
50
- }
51
-
52
- modify (f: (v: TypeOfN<T>) => TypeOfN<T>): (value: T) => Maybe<T> {
53
- return (value: T) => this.of(f(this.get(value)))
54
- }
55
-
56
- unsafeModify (f: (v: TypeOfN<T>) => TypeOfN<T>): (value: T) => T {
57
- return (value: T) => this.unsafeOf(f(this.get(value)))
58
- }
59
- }
package/src/numbers.ts DELETED
@@ -1,222 +0,0 @@
1
- import { lpad } from './strings'
2
-
3
- export const TOLERANCE = 10e-5
4
-
5
- /**
6
- Constant value employed to see if two `number` values are very close.
7
- **/
8
- export const EPSILON = 1e-9
9
-
10
- /**
11
- Returns the angular distance between 2 angles.
12
- **/
13
- export function angleDifference (a: number, b: number, turn = 360.0): number {
14
- let r = (b - a) % turn
15
- if (r < 0) r += turn
16
- if (r > turn / 2) r -= turn
17
- return r
18
- }
19
-
20
- /**
21
- Rounds a number up to the specified number of decimals.
22
- **/
23
- export function ceilTo (v: number, decimals: number): number {
24
- const p = Math.pow(10, decimals)
25
- return Math.ceil(v * p) / p
26
- }
27
-
28
- /**
29
- `clamp` restricts a value within the specified range.
30
- ```ts
31
- log(clamp(1.3, 0, 1)) // prints 1
32
- log(clamp(0.8, 0, 1)) // prints 0.8
33
- log(clamp(-0.5, 0, 1)) // prints 0.0
34
- ```
35
- **/
36
- export function clamp (value: number, min: number, max: number): number {
37
- return Math.min(Math.max(value, min), max)
38
- }
39
-
40
- export function clampInt (value: number, min: number, max: number): number {
41
- return Math.trunc(clamp(value, min, max))
42
- }
43
-
44
- /**
45
- Like clamp but you only pass one argument (`max`) that is used as the upper limit
46
- and the opposite (additive inverse or `-max`) as the lower limit.
47
- **/
48
- export function clampSym (v: number, max: number): number {
49
- return clamp(v, -max, max)
50
- }
51
-
52
- /**
53
- It returns the comparison value (an integer number) between two `float` values.
54
- **/
55
- export function compare (a: number, b: number): number {
56
- return a < b ? -1 : a > b ? 1 : 0
57
- }
58
-
59
- /**
60
- Rounds a number down to the specified number of decimals.
61
- **/
62
- export function floorTo (v: number, decimals: number): number {
63
- const p = Math.pow(10, decimals)
64
- return Math.floor(v * p) / p
65
- }
66
-
67
- /**
68
- `normalize` clamps the passwed value between 0 and 1.
69
- **/
70
- export function normalize (value: number): number {
71
- return clamp(value, 0, 1)
72
- }
73
-
74
- export function toHex (num: number, length = 0): string {
75
- return lpad(num.toString(16), '0', length)
76
- }
77
-
78
- /**
79
- `interpolate` returns a value between `a` and `b` for any value of `t` (normally between 0 and 1).
80
- **/
81
- export function interpolate (a: number, b: number, t: number): number {
82
- return (b - a) * t + a
83
- }
84
-
85
- /**
86
- Interpolates values in a polar coordinate system looking for the narrowest delta angle.
87
- It can be either clock-wise or counter-clock-wise.
88
- **/
89
- export function interpolateAngle (
90
- a: number,
91
- b: number,
92
- t: number,
93
- turn = 360.0
94
- ): number {
95
- return wrapCircular(interpolate(a, a + angleDifference(a, b, turn), t), turn)
96
- }
97
-
98
- /**
99
- Interpolates values in a polar coordinate system looking for the wideset delta angle.
100
- It can be either clock-wise or counter-clock-wise.
101
- **/
102
- export function interpolateAngleWidest (
103
- a: number,
104
- b: number,
105
- t: number,
106
- turn = 360
107
- ): number {
108
- return wrapCircular(interpolateAngle(a, b, t, turn) - turn / 2, turn)
109
- }
110
-
111
- /**
112
- Interpolates values in a polar coordinate system always in clock-wise direction.
113
- **/
114
- export function interpolateAngleCW (
115
- a: number,
116
- b: number,
117
- t: number,
118
- turn = 360
119
- ): number {
120
- a = wrapCircular(a, turn)
121
- b = wrapCircular(b, turn)
122
- if (b < a) b += turn
123
- return wrapCircular(interpolate(a, b, t), turn)
124
- }
125
-
126
- /**
127
- Interpolates values in a polar coordinate system always in counter-clock-wise direction.
128
- **/
129
- export function interpolateAngleCCW (
130
- a: number,
131
- b: number,
132
- t: number,
133
- turn = 360
134
- ): number {
135
- a = wrapCircular(a, turn)
136
- b = wrapCircular(b, turn)
137
- if (b > a) b -= turn
138
- return wrapCircular(interpolate(a, b, t), turn)
139
- }
140
-
141
- /**
142
- number numbers can sometime introduce tiny errors even for simple operations.
143
- `nearEquals` compares two floats using a tiny tollerance (last optional
144
- argument). By default it is defined as `EPSILON`.
145
- **/
146
- export function nearEquals (a: number, b: number, tollerance = EPSILON): boolean {
147
- if (isFinite(a)) {
148
- if (!isFinite(b)) return false
149
- return Math.abs(a - b) <= tollerance
150
- }
151
- if (isNaN(a)) return isNaN(b)
152
- if (isNaN(b)) return false
153
- if (!isFinite(b)) return (a > 0) === (b > 0)
154
- // a is Infinity and b is finite
155
- return false
156
- }
157
-
158
- /**
159
- number numbers can sometime introduce tiny errors even for simple operations.
160
- `nearEqualAngles` compares two angles (default is 360deg) using a tiny
161
- tollerance (last optional argument). By default the tollerance is defined as
162
- `EPSILON`.
163
- **/
164
- export function nearEqualAngles (
165
- a: number,
166
- b: number,
167
- turn = 360.0,
168
- tollerance = EPSILON
169
- ): boolean {
170
- return Math.abs(angleDifference(a, b, turn)) <= tollerance
171
- }
172
-
173
- /**
174
- `nearZero` finds if the passed number is zero or very close to it. By default
175
- `EPSILON` is used as the tollerance value.
176
- **/
177
- export function nearZero (n: number, tollerance = EPSILON): boolean {
178
- return Math.abs(n) <= tollerance
179
- }
180
-
181
- /**
182
- Computes the nth root (`index`) of `base`.
183
- **/
184
- export function root (base: number, index: number): number {
185
- return Math.pow(base, 1 / index)
186
- }
187
-
188
- /**
189
- Rounds a number to the specified number of decimals.
190
- **/
191
- export function roundTo (f: number, decimals: number): number {
192
- const p = Math.pow(10, decimals)
193
- return Math.fround(f * p) / p
194
- }
195
-
196
- /**
197
- `sign` returns `-1` if `value` is a negative number, `1` otherwise.
198
- */
199
- export function sign<T extends number> (value: T): number {
200
- return value < 0 ? -1 : 1
201
- }
202
-
203
- /**
204
- Passed two boundaries values (`min`, `max`), `wrap` ensures that the passed value `v` will
205
- be included in the boundaries. If the value exceeds `max`, the value is reduced by `min`
206
- repeatedely until it falls within the range. Similar and inverted treatment is performed if
207
- the value is below `min`.
208
- **/
209
- export function wrap (v: number, min: number, max: number): number {
210
- const range = max - min + 1
211
- if (v < min) v += range * ((min - v) / range + 1)
212
- return min + ((v - min) % range)
213
- }
214
-
215
- /**
216
- Similar to `wrap`, it works for numbers between 0 and `max`.
217
- **/
218
- export function wrapCircular (v: number, max: number): number {
219
- v = v % max
220
- if (v < 0) v += max
221
- return v
222
- }