@common-fp/shared-internals 0.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.
Files changed (123) hide show
  1. package/dist/a-or-an.cjs +6 -0
  2. package/dist/a-or-an.d.cts +2 -0
  3. package/dist/a-or-an.d.ts +1 -0
  4. package/dist/a-or-an.mjs +6 -0
  5. package/dist/assert-arg-has-values-of-type.cjs +29 -0
  6. package/dist/assert-arg-has-values-of-type.d.cts +2 -0
  7. package/dist/assert-arg-has-values-of-type.d.ts +1 -0
  8. package/dist/assert-arg-has-values-of-type.mjs +29 -0
  9. package/dist/assert-arg-is-array-of-accepted-types.cjs +28 -0
  10. package/dist/assert-arg-is-array-of-accepted-types.d.cts +2 -0
  11. package/dist/assert-arg-is-array-of-accepted-types.d.ts +1 -0
  12. package/dist/assert-arg-is-array-of-accepted-types.mjs +28 -0
  13. package/dist/assert-arg-is-array-of-same-type.cjs +34 -0
  14. package/dist/assert-arg-is-array-of-same-type.d.cts +2 -0
  15. package/dist/assert-arg-is-array-of-same-type.d.ts +1 -0
  16. package/dist/assert-arg-is-array-of-same-type.mjs +34 -0
  17. package/dist/assert-arg-is-array-of-type.cjs +18 -0
  18. package/dist/assert-arg-is-array-of-type.d.cts +2 -0
  19. package/dist/assert-arg-is-array-of-type.d.ts +1 -0
  20. package/dist/assert-arg-is-array-of-type.mjs +18 -0
  21. package/dist/assert-arg-is-char.cjs +19 -0
  22. package/dist/assert-arg-is-char.d.cts +2 -0
  23. package/dist/assert-arg-is-char.d.ts +1 -0
  24. package/dist/assert-arg-is-char.mjs +19 -0
  25. package/dist/assert-arg-is-int.cjs +35 -0
  26. package/dist/assert-arg-is-int.d.cts +2 -0
  27. package/dist/assert-arg-is-int.d.ts +1 -0
  28. package/dist/assert-arg-is-int.mjs +35 -0
  29. package/dist/assert-arg-is-one-of-type.cjs +17 -0
  30. package/dist/assert-arg-is-one-of-type.d.cts +2 -0
  31. package/dist/assert-arg-is-one-of-type.d.ts +1 -0
  32. package/dist/assert-arg-is-one-of-type.mjs +17 -0
  33. package/dist/assert-arg-is-type.cjs +12 -0
  34. package/dist/assert-arg-is-type.d.cts +2 -0
  35. package/dist/assert-arg-is-type.d.ts +1 -0
  36. package/dist/assert-arg-is-type.mjs +12 -0
  37. package/dist/assert-arg-shares-type-with.cjs +12 -0
  38. package/dist/assert-arg-shares-type-with.d.cts +2 -0
  39. package/dist/assert-arg-shares-type-with.d.ts +1 -0
  40. package/dist/assert-arg-shares-type-with.mjs +12 -0
  41. package/dist/assert-arg-type-is-one-of.cjs +14 -0
  42. package/dist/assert-arg-type-is-one-of.d.cts +2 -0
  43. package/dist/assert-arg-type-is-one-of.d.ts +1 -0
  44. package/dist/assert-arg-type-is-one-of.mjs +14 -0
  45. package/dist/common-types.cjs +39 -0
  46. package/dist/common-types.d.cts +2 -0
  47. package/dist/common-types.d.ts +1 -0
  48. package/dist/common-types.mjs +39 -0
  49. package/dist/contains-type.cjs +9 -0
  50. package/dist/contains-type.d.cts +2 -0
  51. package/dist/contains-type.d.ts +1 -0
  52. package/dist/contains-type.mjs +9 -0
  53. package/dist/deps/type-detect.cjs +153 -0
  54. package/dist/deps/type-detect.mjs +153 -0
  55. package/dist/deps/universal-eol.cjs +18 -0
  56. package/dist/deps/universal-eol.mjs +18 -0
  57. package/dist/ensure-number.cjs +10 -0
  58. package/dist/ensure-number.d.cts +2 -0
  59. package/dist/ensure-number.d.ts +1 -0
  60. package/dist/ensure-number.mjs +10 -0
  61. package/dist/ensure-set.cjs +12 -0
  62. package/dist/ensure-set.d.cts +2 -0
  63. package/dist/ensure-set.d.ts +1 -0
  64. package/dist/ensure-set.mjs +12 -0
  65. package/dist/eol.cjs +3 -0
  66. package/dist/eol.d.cts +2 -0
  67. package/dist/eol.d.ts +1 -0
  68. package/dist/eol.mjs +3 -0
  69. package/dist/get-fn-by-type.cjs +9 -0
  70. package/dist/get-fn-by-type.d.cts +2 -0
  71. package/dist/get-fn-by-type.d.ts +1 -0
  72. package/dist/get-fn-by-type.mjs +9 -0
  73. package/dist/get-fn.cjs +11 -0
  74. package/dist/get-fn.d.cts +2 -0
  75. package/dist/get-fn.d.ts +1 -0
  76. package/dist/get-fn.mjs +11 -0
  77. package/dist/get-iterator.cjs +23 -0
  78. package/dist/get-iterator.d.cts +2 -0
  79. package/dist/get-iterator.d.ts +1 -0
  80. package/dist/get-iterator.mjs +23 -0
  81. package/dist/get-random-int.cjs +5 -0
  82. package/dist/get-random-int.d.cts +2 -0
  83. package/dist/get-random-int.d.ts +1 -0
  84. package/dist/get-random-int.mjs +5 -0
  85. package/dist/get-type.cjs +5 -0
  86. package/dist/get-type.d.cts +2 -0
  87. package/dist/get-type.d.ts +1 -0
  88. package/dist/get-type.mjs +5 -0
  89. package/dist/index.cjs +30 -0
  90. package/dist/index.d.cts +2 -0
  91. package/dist/index.d.ts +1 -0
  92. package/dist/index.mjs +28 -0
  93. package/dist/negate.cjs +6 -0
  94. package/dist/negate.d.cts +2 -0
  95. package/dist/negate.d.ts +1 -0
  96. package/dist/negate.mjs +6 -0
  97. package/dist/to-english-list.cjs +12 -0
  98. package/dist/to-english-list.d.cts +2 -0
  99. package/dist/to-english-list.d.ts +1 -0
  100. package/dist/to-english-list.mjs +12 -0
  101. package/dist/truncate-to-n-chars.cjs +18 -0
  102. package/dist/truncate-to-n-chars.d.cts +2 -0
  103. package/dist/truncate-to-n-chars.d.ts +1 -0
  104. package/dist/truncate-to-n-chars.mjs +18 -0
  105. package/dist/validate-array-keys.cjs +19 -0
  106. package/dist/validate-array-keys.d.cts +2 -0
  107. package/dist/validate-array-keys.d.ts +1 -0
  108. package/dist/validate-array-keys.mjs +19 -0
  109. package/dist/validate-is-between-input.cjs +20 -0
  110. package/dist/validate-is-between-input.d.cts +2 -0
  111. package/dist/validate-is-between-input.d.ts +1 -0
  112. package/dist/validate-is-between-input.mjs +20 -0
  113. package/dist/validate-object-keys.cjs +13 -0
  114. package/dist/validate-object-keys.d.cts +2 -0
  115. package/dist/validate-object-keys.d.ts +1 -0
  116. package/dist/validate-object-keys.mjs +13 -0
  117. package/dist/validate-range.cjs +14 -0
  118. package/dist/validate-range.d.cts +2 -0
  119. package/dist/validate-range.d.ts +1 -0
  120. package/dist/validate-range.mjs +14 -0
  121. package/license.txt +106 -0
  122. package/package.json +39 -0
  123. package/readme.md +10 -0
@@ -0,0 +1,6 @@
1
+ const aOrAn = str => {
2
+ const prefix = /[aeiouy]/.test(str[0]) ? 'an' : 'a'
3
+ return `${prefix} ${str}`
4
+ }
5
+
6
+ module.exports = aOrAn
@@ -0,0 +1,2 @@
1
+ import { default as aOrAn } from '@common-fp/shared-internals-types/a-or-an'
2
+ export = aOrAn
@@ -0,0 +1 @@
1
+ export { default } from '@common-fp/shared-internals-types/a-or-an'
@@ -0,0 +1,6 @@
1
+ const aOrAn = str => {
2
+ const prefix = /[aeiouy]/.test(str[0]) ? 'an' : 'a'
3
+ return `${prefix} ${str}`
4
+ }
5
+
6
+ export default aOrAn
@@ -0,0 +1,29 @@
1
+ /**
2
+ * This assumes arg is a collection
3
+ * i.e. arg has already passed assertArgIsOneOfType -> commonTypes.collection
4
+ */
5
+
6
+ const getIterator = require('./get-iterator.cjs')
7
+ const getType = require('./get-type.cjs')
8
+
9
+ const assertArgHasValuesOfType = (arg, argName, type, utilName) => {
10
+ let badTypeFound = false
11
+ let badValType
12
+
13
+ for (const val of getIterator('values', arg)) {
14
+ badValType = getType(val)
15
+ badTypeFound = badValType !== type
16
+ if (badTypeFound) {
17
+ break
18
+ }
19
+ }
20
+
21
+ if (badTypeFound) {
22
+ throw new Error(
23
+ `${utilName} reuqires argument ${argName} to contain only ${type}s` +
24
+ `\nAt least one value was found with type ${badValType}`
25
+ )
26
+ }
27
+ }
28
+
29
+ module.exports = assertArgHasValuesOfType
@@ -0,0 +1,2 @@
1
+ import { default as assertArgHasValuesOfType } from '@common-fp/shared-internals-types/assert-arg-has-values-of-type'
2
+ export = assertArgHasValuesOfType
@@ -0,0 +1 @@
1
+ export { default } from '@common-fp/shared-internals-types/assert-arg-has-values-of-type'
@@ -0,0 +1,29 @@
1
+ /**
2
+ * This assumes arg is a collection
3
+ * i.e. arg has already passed assertArgIsOneOfType -> commonTypes.collection
4
+ */
5
+
6
+ import getIterator from './get-iterator.mjs'
7
+ import getType from './get-type.mjs'
8
+
9
+ const assertArgHasValuesOfType = (arg, argName, type, utilName) => {
10
+ let badTypeFound = false
11
+ let badValType
12
+
13
+ for (const val of getIterator('values', arg)) {
14
+ badValType = getType(val)
15
+ badTypeFound = badValType !== type
16
+ if (badTypeFound) {
17
+ break
18
+ }
19
+ }
20
+
21
+ if (badTypeFound) {
22
+ throw new Error(
23
+ `${utilName} reuqires argument ${argName} to contain only ${type}s` +
24
+ `\nAt least one value was found with type ${badValType}`
25
+ )
26
+ }
27
+ }
28
+
29
+ export default assertArgHasValuesOfType
@@ -0,0 +1,28 @@
1
+ const assertArgIsType = require('./assert-arg-is-type.cjs')
2
+ const getType = require('./get-type.cjs')
3
+ const toEnglishList = require('./to-english-list.cjs')
4
+ const ensureSet = require('./ensure-set.cjs')
5
+
6
+ const assertArgIsArrayOfAcceptedTypes = (
7
+ arg,
8
+ argName,
9
+ acceptedTypes,
10
+ utilName
11
+ ) => {
12
+ assertArgIsType(arg, argName, 'array', utilName)
13
+
14
+ if (!arg.length) return
15
+
16
+ const acceptedTypesStr = toEnglishList('and', acceptedTypes)
17
+
18
+ const setOfAcceptedTypes = ensureSet(acceptedTypes)
19
+ for (const el of arg) {
20
+ const elType = getType(el)
21
+ if (!setOfAcceptedTypes.has(elType)) {
22
+ const msg = `${utilName} requires argument '${argName}' to be an array of ${acceptedTypesStr} - but found ${elType}`
23
+ throw new Error(msg)
24
+ }
25
+ }
26
+ }
27
+
28
+ module.exports = assertArgIsArrayOfAcceptedTypes
@@ -0,0 +1,2 @@
1
+ import { default as assertArgIsArrayOfAcceptedTypes } from '@common-fp/shared-internals-types/assert-arg-is-array-of-accepted-types'
2
+ export = assertArgIsArrayOfAcceptedTypes
@@ -0,0 +1 @@
1
+ export { default } from '@common-fp/shared-internals-types/assert-arg-is-array-of-accepted-types'
@@ -0,0 +1,28 @@
1
+ import assertArgIsType from './assert-arg-is-type.mjs'
2
+ import getType from './get-type.mjs'
3
+ import toEnglishList from './to-english-list.mjs'
4
+ import ensureSet from './ensure-set.mjs'
5
+
6
+ const assertArgIsArrayOfAcceptedTypes = (
7
+ arg,
8
+ argName,
9
+ acceptedTypes,
10
+ utilName
11
+ ) => {
12
+ assertArgIsType(arg, argName, 'array', utilName)
13
+
14
+ if (!arg.length) return
15
+
16
+ const acceptedTypesStr = toEnglishList('and', acceptedTypes)
17
+
18
+ const setOfAcceptedTypes = ensureSet(acceptedTypes)
19
+ for (const el of arg) {
20
+ const elType = getType(el)
21
+ if (!setOfAcceptedTypes.has(elType)) {
22
+ const msg = `${utilName} requires argument '${argName}' to be an array of ${acceptedTypesStr} - but found ${elType}`
23
+ throw new Error(msg)
24
+ }
25
+ }
26
+ }
27
+
28
+ export default assertArgIsArrayOfAcceptedTypes
@@ -0,0 +1,34 @@
1
+ const assertArgIsType = require('./assert-arg-is-type.cjs')
2
+ const ensureSet = require('./ensure-set.cjs')
3
+ const getType = require('./get-type.cjs')
4
+ const toEnglishList = require('./to-english-list.cjs')
5
+
6
+ const assertArgIsArrayOfSameType = (arg, argName, acceptedTypes, utilName) => {
7
+ assertArgIsType(arg, argName, 'array', utilName)
8
+
9
+ if (!arg.length) {
10
+ throw new Error(
11
+ `${utilName} requires argument '${argName}' to have at least one element. This is so it knows which type to return.`
12
+ )
13
+ }
14
+
15
+ const setOfAcceptedTypes = ensureSet(acceptedTypes)
16
+ const acceptedTypesStr = toEnglishList('or', acceptedTypes)
17
+
18
+ const firstType = getType(arg[0])
19
+ for (const el of arg) {
20
+ const elType = getType(el)
21
+ if (!setOfAcceptedTypes.has(elType)) {
22
+ throw new Error(
23
+ `${utilName} requires argument '${argName}' to be an array of ${acceptedTypesStr} - but found ${firstType}`
24
+ )
25
+ }
26
+ if (elType !== firstType) {
27
+ throw new Error(
28
+ `${utilName} requires argument '${argName}' to be an array of the same types - found both ${firstType} and ${elType}`
29
+ )
30
+ }
31
+ }
32
+ }
33
+
34
+ module.exports = assertArgIsArrayOfSameType
@@ -0,0 +1,2 @@
1
+ import { default as assertArgIsArrayOfSameType } from '@common-fp/shared-internals-types/assert-arg-is-array-of-same-type'
2
+ export = assertArgIsArrayOfSameType
@@ -0,0 +1 @@
1
+ export { default } from '@common-fp/shared-internals-types/assert-arg-is-array-of-same-type'
@@ -0,0 +1,34 @@
1
+ import assertArgIsType from './assert-arg-is-type.mjs'
2
+ import ensureSet from './ensure-set.mjs'
3
+ import getType from './get-type.mjs'
4
+ import toEnglishList from './to-english-list.mjs'
5
+
6
+ const assertArgIsArrayOfSameType = (arg, argName, acceptedTypes, utilName) => {
7
+ assertArgIsType(arg, argName, 'array', utilName)
8
+
9
+ if (!arg.length) {
10
+ throw new Error(
11
+ `${utilName} requires argument '${argName}' to have at least one element. This is so it knows which type to return.`
12
+ )
13
+ }
14
+
15
+ const setOfAcceptedTypes = ensureSet(acceptedTypes)
16
+ const acceptedTypesStr = toEnglishList('or', acceptedTypes)
17
+
18
+ const firstType = getType(arg[0])
19
+ for (const el of arg) {
20
+ const elType = getType(el)
21
+ if (!setOfAcceptedTypes.has(elType)) {
22
+ throw new Error(
23
+ `${utilName} requires argument '${argName}' to be an array of ${acceptedTypesStr} - but found ${firstType}`
24
+ )
25
+ }
26
+ if (elType !== firstType) {
27
+ throw new Error(
28
+ `${utilName} requires argument '${argName}' to be an array of the same types - found both ${firstType} and ${elType}`
29
+ )
30
+ }
31
+ }
32
+ }
33
+
34
+ export default assertArgIsArrayOfSameType
@@ -0,0 +1,18 @@
1
+ const assertArgIsType = require('./assert-arg-is-type.cjs')
2
+ const getType = require('./get-type.cjs')
3
+
4
+ const assertArgIsArrayOfType = (arg, argName, type, utilName) => {
5
+ assertArgIsType(arg, argName, 'array', utilName)
6
+
7
+ if (!arg.length) return
8
+
9
+ for (const el of arg) {
10
+ const elType = getType(el)
11
+ if (elType !== type) {
12
+ const errMsg = `${utilName} requires argument '${argName}' to contain only ${type}s - but a ${elType} was found`
13
+ throw new Error(errMsg)
14
+ }
15
+ }
16
+ }
17
+
18
+ module.exports = assertArgIsArrayOfType
@@ -0,0 +1,2 @@
1
+ import { default as assertArgIsArrayOfType } from '@common-fp/shared-internals-types/assert-arg-is-array-of-type'
2
+ export = assertArgIsArrayOfType
@@ -0,0 +1 @@
1
+ export { default } from '@common-fp/shared-internals-types/assert-arg-is-array-of-type'
@@ -0,0 +1,18 @@
1
+ import assertArgIsType from './assert-arg-is-type.mjs'
2
+ import getType from './get-type.mjs'
3
+
4
+ const assertArgIsArrayOfType = (arg, argName, type, utilName) => {
5
+ assertArgIsType(arg, argName, 'array', utilName)
6
+
7
+ if (!arg.length) return
8
+
9
+ for (const el of arg) {
10
+ const elType = getType(el)
11
+ if (elType !== type) {
12
+ const errMsg = `${utilName} requires argument '${argName}' to contain only ${type}s - but a ${elType} was found`
13
+ throw new Error(errMsg)
14
+ }
15
+ }
16
+ }
17
+
18
+ export default assertArgIsArrayOfType
@@ -0,0 +1,19 @@
1
+ const getType = require('./get-type.cjs')
2
+ const truncateToNChars = require('./truncate-to-n-chars.cjs')
3
+
4
+ const assertArgIsChar = (aChar, argName, utilName) => {
5
+ const charType = getType(aChar)
6
+ if (charType !== 'string') {
7
+ throw new Error(
8
+ `${utilName} requires argument '${argName}' to be typeof string - but type ${charType} was passed`
9
+ )
10
+ }
11
+ if (aChar.length !== 1) {
12
+ const truncatedChar = truncateToNChars(10, aChar)
13
+ throw new Error(
14
+ `${utilName} requires argument '${argName}' to be a single character - but ${truncatedChar} was passed`
15
+ )
16
+ }
17
+ }
18
+
19
+ module.exports = assertArgIsChar
@@ -0,0 +1,2 @@
1
+ import { default as assertArgIsChar } from '@common-fp/shared-internals-types/assert-arg-is-char'
2
+ export = assertArgIsChar
@@ -0,0 +1 @@
1
+ export { default } from '@common-fp/shared-internals-types/assert-arg-is-char'
@@ -0,0 +1,19 @@
1
+ import getType from './get-type.mjs'
2
+ import truncateToNChars from './truncate-to-n-chars.mjs'
3
+
4
+ const assertArgIsChar = (aChar, argName, utilName) => {
5
+ const charType = getType(aChar)
6
+ if (charType !== 'string') {
7
+ throw new Error(
8
+ `${utilName} requires argument '${argName}' to be typeof string - but type ${charType} was passed`
9
+ )
10
+ }
11
+ if (aChar.length !== 1) {
12
+ const truncatedChar = truncateToNChars(10, aChar)
13
+ throw new Error(
14
+ `${utilName} requires argument '${argName}' to be a single character - but ${truncatedChar} was passed`
15
+ )
16
+ }
17
+ }
18
+
19
+ export default assertArgIsChar
@@ -0,0 +1,35 @@
1
+ const getType = require('./get-type.cjs')
2
+
3
+ const assertArgIsInt = (n, argName, utilName, furtherAssertions = {}) => {
4
+ const nType = getType(n)
5
+ const { nonNegative, positive } = furtherAssertions
6
+
7
+ if (nType !== 'number') {
8
+ throw new Error(
9
+ `${utilName} requires argument '${argName}' to be an integer - but a ${nType} was passed`
10
+ )
11
+ }
12
+ if (nonNegative) {
13
+ const isNonNegative = Number.isInteger(n) && n >= 0
14
+ if (!isNonNegative) {
15
+ throw new Error(
16
+ `${utilName} requires argument '${argName}' to be an integer greater than or equal to zero - but ${n} was passed`
17
+ )
18
+ }
19
+ } else if (positive) {
20
+ const isPositive = Number.isInteger(n) && n > 0
21
+ if (!isPositive) {
22
+ throw new Error(
23
+ `${utilName} requires argument '${argName}' to be a positive integer - but ${n} was passed`
24
+ )
25
+ }
26
+ } else {
27
+ if (!Number.isInteger(n)) {
28
+ throw new Error(
29
+ `${utilName} requires argument '${argName}' to be an integer - but ${n} was passed`
30
+ )
31
+ }
32
+ }
33
+ }
34
+
35
+ module.exports = assertArgIsInt
@@ -0,0 +1,2 @@
1
+ import { default as assertArgIsInt } from '@common-fp/shared-internals-types/assert-arg-is-int'
2
+ export = assertArgIsInt
@@ -0,0 +1 @@
1
+ export { default } from '@common-fp/shared-internals-types/assert-arg-is-int'
@@ -0,0 +1,35 @@
1
+ import getType from './get-type.mjs'
2
+
3
+ const assertArgIsInt = (n, argName, utilName, furtherAssertions = {}) => {
4
+ const nType = getType(n)
5
+ const { nonNegative, positive } = furtherAssertions
6
+
7
+ if (nType !== 'number') {
8
+ throw new Error(
9
+ `${utilName} requires argument '${argName}' to be an integer - but a ${nType} was passed`
10
+ )
11
+ }
12
+ if (nonNegative) {
13
+ const isNonNegative = Number.isInteger(n) && n >= 0
14
+ if (!isNonNegative) {
15
+ throw new Error(
16
+ `${utilName} requires argument '${argName}' to be an integer greater than or equal to zero - but ${n} was passed`
17
+ )
18
+ }
19
+ } else if (positive) {
20
+ const isPositive = Number.isInteger(n) && n > 0
21
+ if (!isPositive) {
22
+ throw new Error(
23
+ `${utilName} requires argument '${argName}' to be a positive integer - but ${n} was passed`
24
+ )
25
+ }
26
+ } else {
27
+ if (!Number.isInteger(n)) {
28
+ throw new Error(
29
+ `${utilName} requires argument '${argName}' to be an integer - but ${n} was passed`
30
+ )
31
+ }
32
+ }
33
+ }
34
+
35
+ export default assertArgIsInt
@@ -0,0 +1,17 @@
1
+ const aOrAn = require('./a-or-an.cjs')
2
+ const getType = require('./get-type.cjs')
3
+ const toEnglishList = require('./to-english-list.cjs')
4
+ const containsType = require('./contains-type.cjs')
5
+
6
+ const assertArgIsOneOfType = (arg, argName, expectedTypes, utilName) => {
7
+ const type = getType(arg)
8
+
9
+ if (!containsType(expectedTypes, type)) {
10
+ const expectedTypesStr = toEnglishList('or', expectedTypes)
11
+ throw new Error(
12
+ `${utilName} requires argument '${argName}' to be ${aOrAn(expectedTypesStr)} - but ${aOrAn(type)} was passed`
13
+ )
14
+ }
15
+ }
16
+
17
+ module.exports = assertArgIsOneOfType
@@ -0,0 +1,2 @@
1
+ import { default as assertArgIsOneOfType } from '@common-fp/shared-internals-types/assert-arg-is-one-of-type'
2
+ export = assertArgIsOneOfType
@@ -0,0 +1 @@
1
+ export { default } from '@common-fp/shared-internals-types/assert-arg-is-one-of-type'
@@ -0,0 +1,17 @@
1
+ import aOrAn from './a-or-an.mjs'
2
+ import getType from './get-type.mjs'
3
+ import toEnglishList from './to-english-list.mjs'
4
+ import containsType from './contains-type.mjs'
5
+
6
+ const assertArgIsOneOfType = (arg, argName, expectedTypes, utilName) => {
7
+ const type = getType(arg)
8
+
9
+ if (!containsType(expectedTypes, type)) {
10
+ const expectedTypesStr = toEnglishList('or', expectedTypes)
11
+ throw new Error(
12
+ `${utilName} requires argument '${argName}' to be ${aOrAn(expectedTypesStr)} - but ${aOrAn(type)} was passed`
13
+ )
14
+ }
15
+ }
16
+
17
+ export default assertArgIsOneOfType
@@ -0,0 +1,12 @@
1
+ const getType = require('./get-type.cjs')
2
+
3
+ const assertArgIsType = (arg, argName, expectedType, utilName) => {
4
+ const argType = getType(arg)
5
+
6
+ if (argType !== expectedType)
7
+ throw new Error(
8
+ `${utilName} requires argument '${argName}' to be a ${expectedType} - but a ${argType} was passed`
9
+ )
10
+ }
11
+
12
+ module.exports = assertArgIsType
@@ -0,0 +1,2 @@
1
+ import { default as assertArgIsType } from '@common-fp/shared-internals-types/assert-arg-is-type'
2
+ export = assertArgIsType
@@ -0,0 +1 @@
1
+ export { default } from '@common-fp/shared-internals-types/assert-arg-is-type'
@@ -0,0 +1,12 @@
1
+ import getType from './get-type.mjs'
2
+
3
+ const assertArgIsType = (arg, argName, expectedType, utilName) => {
4
+ const argType = getType(arg)
5
+
6
+ if (argType !== expectedType)
7
+ throw new Error(
8
+ `${utilName} requires argument '${argName}' to be a ${expectedType} - but a ${argType} was passed`
9
+ )
10
+ }
11
+
12
+ export default assertArgIsType
@@ -0,0 +1,12 @@
1
+ const assertArgSharesTypeWith = argObj => {
2
+ const { argType, argName, sharedArgType, sharedArgName, utilName } = argObj
3
+
4
+ if (argType !== sharedArgType)
5
+ throw new Error(
6
+ `${utilName} requires the '${argName}' type to match '${sharedArgName}'.\n` +
7
+ ` ${sharedArgName} has type ${sharedArgType}\n` +
8
+ ` ${argName} has type ${argType}`
9
+ )
10
+ }
11
+
12
+ module.exports = assertArgSharesTypeWith
@@ -0,0 +1,2 @@
1
+ import { default as assertArgSharesTypeWith } from '@common-fp/shared-internals-types/assert-arg-shares-type-with'
2
+ export = assertArgSharesTypeWith
@@ -0,0 +1 @@
1
+ export { default } from '@common-fp/shared-internals-types/assert-arg-shares-type-with'
@@ -0,0 +1,12 @@
1
+ const assertArgSharesTypeWith = argObj => {
2
+ const { argType, argName, sharedArgType, sharedArgName, utilName } = argObj
3
+
4
+ if (argType !== sharedArgType)
5
+ throw new Error(
6
+ `${utilName} requires the '${argName}' type to match '${sharedArgName}'.\n` +
7
+ ` ${sharedArgName} has type ${sharedArgType}\n` +
8
+ ` ${argName} has type ${argType}`
9
+ )
10
+ }
11
+
12
+ export default assertArgSharesTypeWith
@@ -0,0 +1,14 @@
1
+ const aOrAn = require('./a-or-an.cjs')
2
+ const toEnglishList = require('./to-english-list.cjs')
3
+ const containsType = require('./contains-type.cjs')
4
+
5
+ const assertArgTypeIsOneOf = (argType, argName, expectedTypes, utilName) => {
6
+ if (!containsType(expectedTypes, argType)) {
7
+ const expectedTypesStr = toEnglishList('or', expectedTypes)
8
+ throw new Error(
9
+ `${utilName} requires argument '${argName}' to be ${aOrAn(expectedTypesStr)} - but ${aOrAn(argType)} was passed`
10
+ )
11
+ }
12
+ }
13
+
14
+ module.exports = assertArgTypeIsOneOf
@@ -0,0 +1,2 @@
1
+ import { default as assertArgTypeIsOneOf } from '@common-fp/shared-internals-types/assert-arg-type-is-one-of'
2
+ export = assertArgTypeIsOneOf
@@ -0,0 +1 @@
1
+ export { default } from '@common-fp/shared-internals-types/assert-arg-type-is-one-of'
@@ -0,0 +1,14 @@
1
+ import aOrAn from './a-or-an.mjs'
2
+ import toEnglishList from './to-english-list.mjs'
3
+ import containsType from './contains-type.mjs'
4
+
5
+ const assertArgTypeIsOneOf = (argType, argName, expectedTypes, utilName) => {
6
+ if (!containsType(expectedTypes, argType)) {
7
+ const expectedTypesStr = toEnglishList('or', expectedTypes)
8
+ throw new Error(
9
+ `${utilName} requires argument '${argName}' to be ${aOrAn(expectedTypesStr)} - but ${aOrAn(argType)} was passed`
10
+ )
11
+ }
12
+ }
13
+
14
+ export default assertArgTypeIsOneOf
@@ -0,0 +1,39 @@
1
+ /**
2
+ * ReadonlySet is a quick naive solution to preventing downstream libs from
3
+ * mutating these values. We can create a more robust solution if/when needed.
4
+ */
5
+
6
+ class ReadonlySet extends Set {
7
+ constructor(iterable) {
8
+ super()
9
+ for (const el of iterable) super.add(el)
10
+ }
11
+
12
+ add() {
13
+ throw new Error('Cannot add to ReadonlySet')
14
+ }
15
+ clear() {
16
+ throw new Error('Cannot clear ReadonlySet')
17
+ }
18
+ delete() {
19
+ throw new Error('Cannot delete with ReadonlySet')
20
+ }
21
+ }
22
+
23
+ const collection = new ReadonlySet(['array', 'object', 'map', 'set'])
24
+ const entryCollection = new ReadonlySet(['array', 'object', 'map'])
25
+ const keyedCollection = new ReadonlySet(['object', 'map'])
26
+ const objectKey = new ReadonlySet(['string', 'number'])
27
+ const propertyKey = new ReadonlySet(['string', 'number', 'symbol'])
28
+ const sequence = new ReadonlySet(['array', 'string'])
29
+ const valueCollection = new ReadonlySet(['array', 'set'])
30
+
31
+ module.exports = {
32
+ collection,
33
+ entryCollection,
34
+ keyedCollection,
35
+ objectKey,
36
+ propertyKey,
37
+ sequence,
38
+ valueCollection,
39
+ }
@@ -0,0 +1,2 @@
1
+ import * as commonTypes from '@common-fp/shared-internals-types/common-types'
2
+ export = commonTypes
@@ -0,0 +1 @@
1
+ export * from '@common-fp/shared-internals-types/common-types'
@@ -0,0 +1,39 @@
1
+ /**
2
+ * ReadonlySet is a quick naive solution to preventing downstream libs from
3
+ * mutating these values. We can create a more robust solution if/when needed.
4
+ */
5
+
6
+ class ReadonlySet extends Set {
7
+ constructor(iterable) {
8
+ super()
9
+ for (const el of iterable) super.add(el)
10
+ }
11
+
12
+ add() {
13
+ throw new Error('Cannot add to ReadonlySet')
14
+ }
15
+ clear() {
16
+ throw new Error('Cannot clear ReadonlySet')
17
+ }
18
+ delete() {
19
+ throw new Error('Cannot delete with ReadonlySet')
20
+ }
21
+ }
22
+
23
+ const collection = new ReadonlySet(['array', 'object', 'map', 'set'])
24
+ const entryCollection = new ReadonlySet(['array', 'object', 'map'])
25
+ const keyedCollection = new ReadonlySet(['object', 'map'])
26
+ const objectKey = new ReadonlySet(['string', 'number'])
27
+ const propertyKey = new ReadonlySet(['string', 'number', 'symbol'])
28
+ const sequence = new ReadonlySet(['array', 'string'])
29
+ const valueCollection = new ReadonlySet(['array', 'set'])
30
+
31
+ export {
32
+ collection,
33
+ entryCollection,
34
+ keyedCollection,
35
+ objectKey,
36
+ propertyKey,
37
+ sequence,
38
+ valueCollection,
39
+ }