@oscarpalmer/atoms 0.176.0 → 0.178.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/src/is.ts CHANGED
@@ -30,15 +30,64 @@ export function isEmpty(value: unknown): boolean {
30
30
  return true;
31
31
  }
32
32
 
33
+ /**
34
+ * Is the value not empty, or holding non-empty values?
35
+ * @param value Value to check
36
+ * @returns `true` if the value is not considered empty, otherwise `false`
37
+ */
38
+ export function isNonEmpty(value: unknown): boolean {
39
+ return !isEmpty(value);
40
+ }
41
+
33
42
  /**
34
43
  * Is the value not `undefined` or `null`?
35
44
  * @param value Value to check
36
45
  * @returns `true` if the value is not `undefined` or `null`, otherwise `false`
37
46
  */
38
- export function isNonNullable(value: unknown): value is Exclude<unknown, undefined | null> {
47
+ export function isNonNullable<Value>(value: Value): value is Exclude<Value, undefined | null> {
39
48
  return value != null;
40
49
  }
41
50
 
51
+ /**
52
+ * Is the value not `undefined`, `null`, or stringified as an empty _(no whitespace)_ string?
53
+ * @param value Value to check
54
+ * @returns `true` if the value is not `undefined`, `null`, or matches an empty string, otherwise `false`
55
+ */
56
+ export function isNonNullableOrEmpty<Value>(
57
+ value: Value,
58
+ ): value is Exclude<Value, undefined | null | ''> {
59
+ return value != null && getString(value) !== '';
60
+ }
61
+
62
+ /**
63
+ * Is the value not `undefined`, `null`, or stringified as a whitespace-only string?
64
+ * @param value Value to check
65
+ * @returns `true` if the value is not `undefined`, `null`, or matches a whitespace-only string, otherwise `false`
66
+ */
67
+ export function isNonNullableOrWhitespace<Value>(
68
+ value: Value,
69
+ ): value is Exclude<Value, undefined | null | ''> {
70
+ return value != null && !EXPRESSION_WHITESPACE.test(getString(value));
71
+ }
72
+
73
+ /**
74
+ * Is the value not a number or a number-like string?
75
+ * @param value Value to check
76
+ * @returns `true` if the value is not a number or a number-like string, otherwise `false`
77
+ */
78
+ export function isNonNumerical<Value>(value: Value): value is Exclude<Value, number | `${number}`> {
79
+ return !isNumerical(value);
80
+ }
81
+
82
+ /**
83
+ * Is the value not an object _(or function)_?
84
+ * @param value Value to check
85
+ * @returns `true` if the value is not an object, otherwise `false`
86
+ */
87
+ export function isNonObject<Value>(value: Value): value is Exclude<Value, object> {
88
+ return !isObject(value);
89
+ }
90
+
42
91
  /**
43
92
  * Is the value `undefined` or `null`?
44
93
  * @param value Value to check
@@ -49,18 +98,18 @@ export function isNullable(value: unknown): value is undefined | null {
49
98
  }
50
99
 
51
100
  /**
52
- * Is the value `undefined`, `null`, or an empty _(no whitespace)_ string?
101
+ * Is the value `undefined`, `null`, or stringified as an empty _(no whitespace)_ string?
53
102
  * @param value Value to check
54
- * @returns `true` if the value is nullable or an empty string, otherwise `false`
103
+ * @returns `true` if the value is nullable or matches an empty string, otherwise `false`
55
104
  */
56
105
  export function isNullableOrEmpty(value: unknown): value is undefined | null | '' {
57
106
  return value == null || getString(value) === '';
58
107
  }
59
108
 
60
109
  /**
61
- * Is the value `undefined`, `null`, or a whitespace-only string?
110
+ * Is the value `undefined`, `null`, or stringified as a whitespace-only string?
62
111
  * @param value Value to check
63
- * @returns `true` if the value is nullable or a whitespace-only string, otherwise `false`
112
+ * @returns `true` if the value is nullable or matches a whitespace-only string, otherwise `false`
64
113
  */
65
114
  export function isNullableOrWhitespace(value: unknown): value is undefined | null | '' {
66
115
  return value == null || EXPRESSION_WHITESPACE.test(getString(value));
@@ -69,7 +118,7 @@ export function isNullableOrWhitespace(value: unknown): value is undefined | nul
69
118
  /**
70
119
  * Is the value a number or a number-like string?
71
120
  * @param value Value to check
72
- * @returns `true` if the value is a number or a parseable string, otherwise `false`
121
+ * @returns `true` if the value is a number or a number-like string, otherwise `false`
73
122
  */
74
123
  export function isNumerical(value: unknown): value is number | `${number}` {
75
124
  return (
@@ -102,6 +151,14 @@ export {
102
151
  isConstructor,
103
152
  isInstanceOf,
104
153
  isKey,
154
+ isNonArrayOrPlainObject,
155
+ isNonConstructor,
156
+ isNonInstanceOf,
157
+ isNonKey,
158
+ isNonNumber,
159
+ isNonPlainObject,
160
+ isNonPrimitive,
161
+ isNonTypedArray,
105
162
  isNumber,
106
163
  isPlainObject,
107
164
  isPrimitive,
@@ -1,5 +1,63 @@
1
+ import {isTemplateStringsArray} from '../internal/is';
2
+ import {getString} from '../internal/string';
3
+
1
4
  // #region Functions
2
5
 
6
+ export function dedent(strings: TemplateStringsArray, ...values: unknown[]): string;
7
+
8
+ export function dedent(value: string): string;
9
+
10
+ export function dedent(value: string | TemplateStringsArray, ...values: unknown[]): string {
11
+ let actual: string;
12
+
13
+ if (isTemplateStringsArray(value)) {
14
+ actual = interpolate(value, values);
15
+ } else {
16
+ actual = value;
17
+ }
18
+
19
+ if (typeof actual !== 'string') {
20
+ return '';
21
+ }
22
+
23
+ const lines = actual.split('\n');
24
+ const {length} = lines;
25
+
26
+ if (length === 1) {
27
+ return actual.trim();
28
+ }
29
+
30
+ const lastIndex = length - 1;
31
+
32
+ const lengths: number[] = [];
33
+
34
+ for (let index = 0; index < length; index += 1) {
35
+ const [, indentation] = /^(\s+)/.exec(lines[index]) ?? [];
36
+
37
+ if (indentation != null) {
38
+ lengths.push(indentation.length);
39
+ }
40
+ }
41
+
42
+ if (lengths.length === 0) {
43
+ return actual.trim();
44
+ }
45
+
46
+ const minimum = Math.min(...lengths);
47
+
48
+ const pattern = new RegExp(`^\\s{0,${minimum}}`);
49
+
50
+ let result = '';
51
+
52
+ for (let index = 0; index < length; index += 1) {
53
+ const line = lines[index];
54
+
55
+ result += line.replace(pattern, '') + (index === lastIndex ? '' : '\n');
56
+ }
57
+
58
+ return result.trim();
59
+ }
60
+
3
61
  /**
4
62
  * Get a new UUID-string _(version 4)_
5
63
  * @returns UUID string
@@ -24,6 +82,18 @@ export function getUuid(): string {
24
82
  ].join('-');
25
83
  }
26
84
 
85
+ function interpolate(strings: TemplateStringsArray, values: unknown[]): string {
86
+ const {length} = strings;
87
+
88
+ let interpolated = '';
89
+
90
+ for (let index = 0; index < length; index += 1) {
91
+ interpolated += `${strings[index]}${getString(values[index])}`;
92
+ }
93
+
94
+ return interpolated;
95
+ }
96
+
27
97
  /**
28
98
  * Parse a JSON string into its proper value _(or `undefined` if it fails)_
29
99
  * @param value JSON string to parse
@@ -78,6 +148,8 @@ export function truncate(value: string, length: number, suffix?: string): string
78
148
  return `${value.slice(0, truncatedLength)}${actualSuffix}`;
79
149
  }
80
150
 
151
+ // #endregion
152
+
81
153
  // #region Variables
82
154
 
83
155
  const ZERO = '0';
@@ -86,4 +158,8 @@ const ZERO = '0';
86
158
 
87
159
  // #endregion
88
160
 
161
+ // #region Exports
162
+
89
163
  export {getString, join, words} from '../internal/string';
164
+
165
+ // #endregion