atomic-fns 0.3.3 → 0.3.4

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 (43) hide show
  1. package/README.md +17 -3
  2. package/core/collections/LRUCache.d.ts +22 -1
  3. package/core/collections/LRUCache.js +22 -1
  4. package/core/collections/LRUCache.ts +22 -1
  5. package/core/collections/SortedMap.d.ts +1 -1
  6. package/core/collections/SortedMap.js +1 -1
  7. package/core/collections/SortedMap.ts +1 -1
  8. package/core/collections/SortedSet.d.ts +6 -0
  9. package/core/collections/SortedSet.js +6 -0
  10. package/core/collections/SortedSet.ts +6 -0
  11. package/core/collections/SortedTree.d.ts +3 -3
  12. package/core/collections/SortedTree.js +3 -3
  13. package/core/collections/SortedTree.ts +3 -3
  14. package/core/collections/index.d.ts +2 -0
  15. package/core/collections/index.js +7 -5
  16. package/core/collections/index.ts +7 -5
  17. package/core/functools/index.d.ts +2 -2
  18. package/core/functools/index.ts +6 -3
  19. package/core/globals/index.d.ts +22 -3
  20. package/core/globals/index.js +28 -5
  21. package/core/globals/index.ts +39 -6
  22. package/core/i18n/date/format.d.ts +16 -0
  23. package/core/i18n/date/format.js +307 -0
  24. package/core/i18n/date/format.ts +343 -0
  25. package/core/i18n/date/index.d.ts +223 -0
  26. package/core/i18n/date/index.js +387 -0
  27. package/core/i18n/date/index.ts +471 -0
  28. package/core/i18n/date/test.html +67 -0
  29. package/core/i18n/index.d.ts +7 -0
  30. package/core/i18n/index.js +7 -0
  31. package/core/i18n/index.ts +8 -0
  32. package/core/index.d.ts +1 -0
  33. package/core/index.js +1 -0
  34. package/core/index.ts +1 -0
  35. package/core/operators/index.d.ts +1 -1
  36. package/core/operators/index.js +35 -5
  37. package/core/operators/index.ts +34 -6
  38. package/package.json +11 -11
  39. package/tests/globals.spec.ts +9 -9
  40. package/tsconfig.json +2 -1
  41. package/core/datetime/index.d.ts +0 -45
  42. package/core/datetime/index.js +0 -71
  43. package/core/datetime/index.ts +0 -128
@@ -142,15 +142,25 @@ export function type(value) {
142
142
  */
143
143
  export const str = (obj) => (obj != null ? obj.toString() : '')
144
144
 
145
+ export function hasOwnProperty(obj, prop) {
146
+ return Object.prototype.hasOwnProperty.call(obj, prop)
147
+ }
148
+
145
149
  /** Check if value is a boolean type. */
146
150
  export const isBool = (x): x is boolean => type(x) === 'boolean'
147
151
 
152
+ /** Check if value is an `Error` type. */
153
+ export const isError = (x): x is Error => x instanceof Error
154
+
148
155
  /** Check if value is an iterable type. */
149
156
  export const isIterable = (x): x is IterableIterator<any> => {
150
157
  if (x == null) return false
151
158
  return typeof x[Symbol.iterator] === 'function'
152
159
  }
153
160
 
161
+ /** Check if value is a native `Date` type. */
162
+ export const isDate = (x): x is Date => x instanceof Date
163
+
154
164
  /** Check if value is an object type. */
155
165
  export const isObject = (x): x is Object => typeof x === 'object' && type(x) === 'object'
156
166
 
@@ -169,12 +179,12 @@ export const isArrayLike = (x) => {
169
179
  return T === 'array' || (T === 'object' && isNumber(x.length))
170
180
  }
171
181
 
172
- /** Check if value is a function type. */
173
- export const isFunc = (x): x is Function => x?.constructor === Function
174
-
175
182
  /** Check if value is a number type. */
176
183
  export const isNumber = (x): x is number => type(x) === 'number'
177
184
 
185
+ /** Check if value is an integer number type. */
186
+ export const isInteger = (x): x is number => type(x) === 'number' && Math.floor(x) === x
187
+
178
188
  /** Check if value is a bigint type. */
179
189
  export const isBigint = (x): x is BigInt => type(x) === 'bigint'
180
190
 
@@ -184,8 +194,11 @@ export const isNaN = (x): x is typeof NaN => Number.isNaN(x)
184
194
  /** Check if value is a Promise type. */
185
195
  export const isPromise = (x): x is Promise<any> => type(x) === 'Promise'
186
196
 
197
+ /** Check if value is a function type. */
198
+ export const isFunction = (x): x is Function => x?.constructor === Function
199
+
187
200
  /** Check if value is an async function type. */
188
- export const isAsync = (x) => x?.constructor.name === 'AsyncFunction'
201
+ export const isAsyncFunction = (x) => x?.constructor.name === 'AsyncFunction'
189
202
 
190
203
  /** Check if value is a generator function type. */
191
204
  export const isGenerator = (x): x is Generator => {
@@ -195,15 +208,35 @@ export const isGenerator = (x): x is Generator => {
195
208
  /** Check if value is `null` or `undefined`. */
196
209
  export const isNull = (x): x is null | undefined => x == null
197
210
 
211
+ /** Check if value === `undefined`. */
212
+ export function isUndefined(o) {
213
+ return typeof o === 'undefined'
214
+ }
215
+
198
216
  /** Check if value is not `null` or `undefined`. */
199
217
  export const notNull = (x) => x != null
200
218
 
201
219
  /** Returns `true` for objects without length or falsy values. */
202
220
  export const isEmpty = (x) => (len(x) === 0 ? true : !x)
203
221
 
222
+ /** Check if value is a `RegExp` type */
223
+ export const isRegExp = (x): x is RegExp => x instanceof RegExp
224
+
204
225
  /** Check if value is a `Symbol` type */
205
226
  export const isSymbol = (x): x is symbol => type(x) === 'symbol'
206
227
 
228
+ /** Check if value is a `Set` type. */
229
+ export const isSet = <T>(x): x is Set<T> => x instanceof Set
230
+
231
+ /** Check if value is a `Map` type. */
232
+ export const isMap = <K, V>(x): x is Map<K, V> => x instanceof Map
233
+
234
+ /** Check if value is a `WeakSet` type. */
235
+ export const isWeakSet = <T extends object>(x): x is WeakSet<T> => x instanceof WeakSet
236
+
237
+ /** Check if value is a `WeakMap` type. */
238
+ export const isWeakMap = <K extends object, V>(x): x is WeakMap<K, V> => x instanceof WeakMap
239
+
207
240
  /**
208
241
  * Returns the number of elements in a collection type.
209
242
  *
@@ -217,7 +250,7 @@ export function len(value) {
217
250
  if (isNumber(value.length)) {
218
251
  return value.length
219
252
  }
220
- if (isFunc(value.size)) {
253
+ if (isFunction(value.size)) {
221
254
  return value.size()
222
255
  }
223
256
  if (isNumber(value.size)) {
@@ -267,7 +300,7 @@ export const uuid = () => {
267
300
  * @return `obj.key(...args)` or `undefined`.
268
301
  */
269
302
  export function call(obj: any, key: PropertyKey, ...args: any[]) {
270
- if (isFunc(get(key, obj))) {
303
+ if (isFunction(get(key, obj))) {
271
304
  return obj[key](...args)
272
305
  }
273
306
  }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Returns a new Date from the given arguments.
3
+ * @param {string | number | Date} date The date value
4
+ * @param {?boolean} [utc=false] Interprets the given value as a UTC date
5
+ * @returns {Date} The new date object
6
+ */
7
+ export declare function getDate(date: string | number | Date, utc?: boolean): Date;
8
+ /**
9
+ * Formats a date using a formatting string in the {@link https://strftime.org/ strftime} format, in any given locale.
10
+ * @param {string} fmt The format string to apply
11
+ * @param {number | Date} date The date value
12
+ * @param {?string} locale The locale to use when formatting (default is system locale).
13
+ * @returns {string} The string representation of date
14
+ * @see {@link https://strftime.org/ strftime format}
15
+ */
16
+ export declare function format(fmt: string, date: number | Date, locale?: any): string;
@@ -0,0 +1,307 @@
1
+ const NATIVE_DATE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/;
2
+ /**
3
+ * Returns a new Date from the given arguments.
4
+ * @param {string | number | Date} date The date value
5
+ * @param {?boolean} [utc=false] Interprets the given value as a UTC date
6
+ * @returns {Date} The new date object
7
+ */
8
+ export function getDate(date, utc = false) {
9
+ if (date === null)
10
+ return new Date(NaN);
11
+ if (!date)
12
+ return new Date();
13
+ if (date instanceof Date)
14
+ return new Date(date);
15
+ if (typeof date === 'string' && !/Z$/i.test(date)) {
16
+ const d = date.match(NATIVE_DATE);
17
+ if (d) {
18
+ const m = d[2] - 1 || 0;
19
+ const ms = (d[7] || '0').slice(0, 3);
20
+ if (utc) {
21
+ return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms));
22
+ }
23
+ return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms);
24
+ }
25
+ }
26
+ return new Date(date);
27
+ }
28
+ const n = 'numeric', s = 'short', l = 'long', dd = '2-digit', TOKEN_MAP = {
29
+ // weekday
30
+ '%A': { weekday: l },
31
+ '%a': { weekday: s },
32
+ '%aa': { weekday: n },
33
+ // day
34
+ '%d': { day: dd },
35
+ '%-d': { day: n },
36
+ // month
37
+ '%m': { month: dd },
38
+ '%-m': { month: n },
39
+ '%B': { month: l },
40
+ '%b': { month: s },
41
+ '%bb': { month: n },
42
+ // year
43
+ '%y': { year: n },
44
+ '%Y': { year: dd },
45
+ // hour
46
+ '%K': { hourCycle: 'h11', hour: dd },
47
+ '%I': { hourCycle: 'h12', hour: dd },
48
+ '%-I': { hourCycle: 'h12', hour: n },
49
+ '%H': { hourCycle: 'h23', hour: dd },
50
+ '%-H': { hourCycle: 'h23', hour: n },
51
+ '%k': { hourCycle: 'h24', hour: n },
52
+ // minutes
53
+ '%M': { minute: dd },
54
+ '%-M': { minute: n },
55
+ // seconds
56
+ '%S': { second: dd },
57
+ '%-S': { second: n },
58
+ // day period
59
+ '%P': { dayPeriod: l },
60
+ '%p': { dayPeriod: s },
61
+ '%pp': { dayPeriod: 'narrow' },
62
+ // era
63
+ '%N': { era: l, year: dd },
64
+ '%n': { era: s, year: dd },
65
+ '%nn': { era: n, year: 'narrow' },
66
+ // timeZoneName
67
+ '%Z': { timeZoneName: 'longGeneric', year: dd },
68
+ '%-Z': { timeZoneName: 'shortGeneric', year: dd },
69
+ '%z': { timeZoneName: 'longOffset', year: dd },
70
+ '%-z': { timeZoneName: 'shortOffset', year: dd },
71
+ '%ZZ': { timeZoneName: l, year: dd },
72
+ '%-ZZ': { timeZoneName: s, year: dd }
73
+ };
74
+ const HOUR_PARTS = { '%I': 1, '%-I': 1, '%K': 1, '%k': 1 };
75
+ const ERA_PARTS = { '%n': 1, '%N': 1, '%nn': 1 };
76
+ const TZ_PARTS = { '%ZZ': 1, '%-ZZ': 1, '%-z': 1, '%z': 1, '%-Z': 1, '%Z': 1 };
77
+ const formatter = (locale, opts) => new Intl.DateTimeFormat(locale, opts);
78
+ /**
79
+ * Formats a date using a formatting string in the {@link https://strftime.org/ strftime} format, in any given locale.
80
+ * @param {string} fmt The format string to apply
81
+ * @param {number | Date} date The date value
82
+ * @param {?string} locale The locale to use when formatting (default is system locale).
83
+ * @returns {string} The string representation of date
84
+ * @see {@link https://strftime.org/ strftime format}
85
+ */
86
+ export function format(fmt, date, locale) {
87
+ let results = [], dayPeriod = '', dayPeriodIndex = -1;
88
+ for (let i = 0; i < fmt.length; i++) {
89
+ let part = fmt[i];
90
+ if (part === '%') {
91
+ // check for escaped '%'
92
+ if (fmt[++i] == '%') {
93
+ results.push('%');
94
+ continue;
95
+ }
96
+ if (fmt[i] === '-') {
97
+ part += fmt[i++];
98
+ }
99
+ part += fmt[i];
100
+ // add any repeated characters that come after
101
+ while (i < fmt.length - 1 && fmt[i + 1] === fmt[i])
102
+ part += fmt[++i];
103
+ if (HOUR_PARTS[part]) {
104
+ let res = formatter(locale, TOKEN_MAP[part]).format(date).split(' ');
105
+ // save the AM/PM for the correct spot
106
+ results.push(res[0]);
107
+ dayPeriod = res[1];
108
+ continue;
109
+ }
110
+ if (part === '%P' || part === '%p') {
111
+ // leave this flag in place for later
112
+ dayPeriodIndex = results.length;
113
+ results.push(part);
114
+ continue;
115
+ }
116
+ if (ERA_PARTS[part] || TZ_PARTS[part]) {
117
+ // the formatting includes the date/year so we take what comes after
118
+ let res = formatter(locale, TOKEN_MAP[part]).format(date).split(' ');
119
+ results.push(res.slice(1).join(' '));
120
+ continue;
121
+ }
122
+ // format any other parts
123
+ let res = formatter(locale, TOKEN_MAP[part]).format(date);
124
+ if (part === '%S' && res.length === 1)
125
+ res = '0' + res;
126
+ results.push(res);
127
+ }
128
+ else {
129
+ // output as literal
130
+ results.push(part);
131
+ }
132
+ }
133
+ // insert the AM/PM at the correct spot
134
+ if (dayPeriodIndex >= 0)
135
+ results[dayPeriodIndex] = dayPeriod || '';
136
+ return results.join('');
137
+ }
138
+ const DATETIME_DEFAULT = {};
139
+ /** 10/14/1983 */
140
+ const DATE_SHORT = {
141
+ year: n,
142
+ month: n,
143
+ day: n
144
+ };
145
+ /** Oct 14, 1983 */
146
+ const DATE_MED = {
147
+ year: n,
148
+ month: s,
149
+ day: n
150
+ };
151
+ /** Fri, Oct 14, 1983 */
152
+ const DATE_MED_WITH_WEEKDAY = {
153
+ year: n,
154
+ month: s,
155
+ day: n,
156
+ weekday: s
157
+ };
158
+ /** October 14, 1983 */
159
+ const DATE_FULL = {
160
+ year: n,
161
+ month: l,
162
+ day: n
163
+ };
164
+ /** Tuesday, October 14, 1983 */
165
+ const DATE_HUGE = {
166
+ year: n,
167
+ month: l,
168
+ day: n,
169
+ weekday: l
170
+ };
171
+ /** 09:30 AM */
172
+ const TIME_SIMPLE = {
173
+ hour: n,
174
+ minute: n
175
+ };
176
+ /** 09:30:23 AM */
177
+ const TIME_WITH_SECONDS = {
178
+ hour: n,
179
+ minute: n,
180
+ second: n
181
+ };
182
+ /** 09:30:23 AM EDT */
183
+ const TIME_WITH_SHORT_OFFSET = {
184
+ hour: n,
185
+ minute: n,
186
+ second: n,
187
+ timeZoneName: s
188
+ };
189
+ /** 09:30:23 AM Eastern Daylight Time */
190
+ const TIME_WITH_LONG_OFFSET = {
191
+ hour: n,
192
+ minute: n,
193
+ second: n,
194
+ timeZoneName: l
195
+ };
196
+ /** 09:30 */
197
+ const TIME_24_SIMPLE = {
198
+ hour: n,
199
+ minute: n,
200
+ hourCycle: 'h23'
201
+ };
202
+ /** 09:30:23 */
203
+ const TIME_24_WITH_SECONDS = {
204
+ hour: n,
205
+ minute: n,
206
+ second: n,
207
+ hourCycle: 'h23'
208
+ };
209
+ /** 09:30:23 EDT */
210
+ const TIME_24_WITH_SHORT_OFFSET = {
211
+ hour: n,
212
+ minute: n,
213
+ second: n,
214
+ hourCycle: 'h23',
215
+ timeZoneName: s
216
+ };
217
+ /** 09:30:23 Eastern Daylight Time */
218
+ const TIME_24_WITH_LONG_OFFSET = {
219
+ hour: n,
220
+ minute: n,
221
+ second: n,
222
+ hourCycle: 'h23',
223
+ timeZoneName: l
224
+ };
225
+ /** 10/14/1983, 9:30 AM */
226
+ const DATETIME_SHORT = {
227
+ year: n,
228
+ month: n,
229
+ day: n,
230
+ hour: n,
231
+ minute: n
232
+ };
233
+ /** 10/14/1983, 9:30:33 AM */
234
+ const DATETIME_SHORT_WITH_SECONDS = {
235
+ year: n,
236
+ month: n,
237
+ day: n,
238
+ hour: n,
239
+ minute: n,
240
+ second: n
241
+ };
242
+ /** Oct 14, 1983, 9:30 AM */
243
+ const DATETIME_MED = {
244
+ year: n,
245
+ month: s,
246
+ day: n,
247
+ hour: n,
248
+ minute: n
249
+ };
250
+ /** Oct 14, 1983, 9:30:33 AM */
251
+ const DATETIME_MED_WITH_SECONDS = {
252
+ year: n,
253
+ month: s,
254
+ day: n,
255
+ hour: n,
256
+ minute: n,
257
+ second: n
258
+ };
259
+ /** Fri, 14 Oct 1983, 9:30 AM */
260
+ const DATETIME_MED_WITH_WEEKDAY = {
261
+ year: n,
262
+ month: s,
263
+ day: n,
264
+ weekday: s,
265
+ hour: n,
266
+ minute: n
267
+ };
268
+ /** October 14, 1983, 9:30 AM EDT */
269
+ const DATETIME_FULL = {
270
+ year: n,
271
+ month: l,
272
+ day: n,
273
+ hour: n,
274
+ minute: n,
275
+ timeZoneName: s
276
+ };
277
+ /** October 14, 1983, 9:30:33 AM EDT */
278
+ const DATETIME_FULL_WITH_SECONDS = {
279
+ year: n,
280
+ month: l,
281
+ day: n,
282
+ hour: n,
283
+ minute: n,
284
+ second: n,
285
+ timeZoneName: s
286
+ };
287
+ /** Friday, October 14, 1983, 9:30 AM Eastern Daylight Time */
288
+ const DATETIME_HUGE = {
289
+ year: n,
290
+ month: l,
291
+ day: n,
292
+ weekday: l,
293
+ hour: n,
294
+ minute: n,
295
+ timeZoneName: l
296
+ };
297
+ /** Friday, October 14, 1983, 9:30:33 AM Eastern Daylight Time */
298
+ const DATETIME_HUGE_WITH_SECONDS = {
299
+ year: n,
300
+ month: l,
301
+ day: n,
302
+ weekday: l,
303
+ hour: n,
304
+ minute: n,
305
+ second: n,
306
+ timeZoneName: l
307
+ };