@valkyriestudios/utils 11.7.0 → 12.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.
package/README.md CHANGED
@@ -155,7 +155,7 @@ shuffle(arr);
155
155
  // [4, 6, 3, 2, 5, 1]
156
156
  ```
157
157
 
158
- - **sort(val:Array[object], by:String|Function, dir:Enum(asc,desc), options:Object)**
158
+ - **sort(val:Array[object], by:string|Function, dir:Enum(asc,desc), options:Object)**
159
159
  Sort an array of objects, uses an implementation of [Tony Hoare's quicksort](https://cs.stanford.edu/people/eroberts/courses/soco/projects/2008-09/tony-hoare/quicksort.html)
160
160
 
161
161
  ```js
@@ -279,7 +279,7 @@ isDate(new Date('December 17, 1995 03:24:00'); // TRUE
279
279
  isDate('December 17, 1995 03:24:00'); // FALSE
280
280
  ```
281
281
 
282
- - **diff(val_a:Date, val_b:Date, key:String)**
282
+ - **diff(val_a:Date, val_b:Date, key:string)**
283
283
  Take two incoming dates and return the difference between them in a certain unit. Possible key options(week,day,hour,minute,second,millisecond).
284
284
 
285
285
  Note: Does not touch the passed date objects, if no key is passed will default to millisecond
@@ -292,6 +292,51 @@ diff(new Date("2022-10-05T13:12:11+02:00"), new Date("2022-10-05T17:43:09.344+06
292
292
  diff(new Date("2022-10-05T13:12:11+02:00"), new Date("2022-10-05T17:43:09.344+06:00"), 'millisecond'); // -1858344
293
293
  diff(new Date("2022-11-05T13:12:11+06:00"), new Date("2022-10-05T13:25:43.898+02:00")); // 2663187102
294
294
  ```
295
+ - **format(val:Date, spec:string, locale?:string, zone?:string):string**
296
+ Format a date according to a spec/locale and zone
297
+
298
+ Note: The locale is by default set to 'en-US'
299
+
300
+ Note: The zone is by default detected as the zone of the client
301
+
302
+ Available tokens for usage in spec:
303
+ | Token | Description | Example |
304
+ |:---------|:--------------------------|:---------------|
305
+ | `YYYY` | Full Year | 2021 |
306
+ | `Q` | Quarters of the year | 1 2 3 4 |
307
+ | `MMMM` | Month in full | January February ... November December |
308
+ | `MMM` | Month as 3 char | Jan Feb ... Nov Dec |
309
+ | `MM` | Month as 2 char | 01 02 .. 11 12 |
310
+ | `M` | Month as pure digit | 1 2 .. 11 12 |
311
+ | `DD` | Day of month as 2 char | 01 02 .. 30 31 |
312
+ | `D` | Day of month as 1 char | 1 2 .. 30 31 |
313
+ | `dddd` | Day of week as 3 char | Sun Mon ... Fri Sat |
314
+ | `ddd` | Day of week in full | Sunday Monday ... Saturday |
315
+ | `HH` | Hours as 2-char | 00 01 .. 22 23 |
316
+ | `H` | Hours as pure digit | 0 1 .. 22 23 |
317
+ | `hh` | Hours in 12 hour time as 2 char | 01 02 ... 11 12 |
318
+ | `h` | Hours in 12 hour time as pure digit | 1 2 ... 11 12 |
319
+ | `mm` | Minutes as 2-char | 00 01 .. 58 59 |
320
+ | `m` | Minutes as pure digit | 0 1 .. 58 59 |
321
+ | `ss` | Seconds as 2-char | 00 01 .. 58 59 |
322
+ | `s` | Seconds as pure digit | 0 1 .. 58 59 |
323
+ | `SSS` | Milliseconds as 3-digit | 000 001 ... 998 999 |
324
+ | `A` | Uppercase AM/PM | AM ... PM |
325
+ | `a` | Lowercase AM/PM | am ... pm |
326
+
327
+ ```js
328
+ format(new Date('2023-01-10T14:30:00Z'), '[Today is] dddd, MMMM D, YYYY [at] h:mm A', 'en', 'Europe/Brussels');
329
+ // 'Today is Tuesday, January 10, 2023 at 2:30 PM'
330
+
331
+ format(new Date('2022-07-14T16:40:30Z'), 'dddd, [Year] Q Q M D [à] hh:mm A [string]', 'fr', 'Asia/Singapore');
332
+ // 'vendredi, Year 3 3 7 15 à 12:40 AM string'
333
+
334
+ format(new Date('2022-07-14T16:40:30Z'), 'YYYY-MM-DD', 'fr', 'Asia/Singapore');
335
+ // 2022-07-15
336
+
337
+ format(new Date('2022-07-14T16:40:30Z'), 'YYYY-MM-DD', 'fr', 'Europe/Brussels');
338
+ // 2022-07-14
339
+ ```
295
340
 
296
341
  - **toUTC(val:Date)**
297
342
  Takes the passed date object and returns a new date object set for utc
@@ -305,7 +350,7 @@ Returns the current unix timestamp in seconds
305
350
  - **nowUnixMs()**
306
351
  Returns the current unix timestamp in milliseconds
307
352
 
308
- - **startOfUTC(val:Date, key:String)**
353
+ - **startOfUTC(val:Date, key:string)**
309
354
  Take the incoming date and return a date set to the start of passed key. Possible key options(year,quarter,month,week,week_sun,week_mon,week_tue,week_wed,week_thu,week_fri,week_sat,day,hour,minute,second).
310
355
 
311
356
  Note: Does not touch the date object passed
@@ -325,7 +370,7 @@ startOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'minute'); // new Date("2023-0
325
370
  startOfUTC(new Date("2023-05-04T12:04:27.043+02:00"), 'second'); // new Date("2023-05-04T10:04:27.000Z")
326
371
  ```
327
372
 
328
- - **endOfUTC(val:Date, key:String)**
373
+ - **endOfUTC(val:Date, key:string)**
329
374
  Take the incoming date and return a date set to the end of passed key. Possible key options(year,quarter,month,week,week_sun,week_mon,week_tue,week_wed,week_thu,week_fri,week_sat,day,hour,minute,second).
330
375
 
331
376
  Note: Does not touch the date object passed
@@ -349,7 +394,7 @@ endOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'minute'); // new Date("2023-05-
349
394
  endOfUTC(new Date("2023-05-04T12:04:27.043+02:00"), 'second'); // new Date("2023-05-04T10:04:27.999Z")
350
395
  ```
351
396
 
352
- - **addUTC(val:Date, amount:integer, key:String)**
397
+ - **addUTC(val:Date, amount:integer, key:string)**
353
398
  Take the incoming date and add a certain amount of the passed key. Possible key options(year,years,month,months,day,days,hour,hours,minute,minutes,second,seconds,millisecond,milliseconds).
354
399
 
355
400
  Note: Does not touch the date object passed
package/array/dedupe.js CHANGED
@@ -1,19 +1,13 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const fnv1A_1 = require("../hash/fnv1A");
4
3
  function dedupe(val) {
5
- if (!Array.isArray(val) || val.length === 0)
4
+ if (!Array.isArray(val))
6
5
  return [];
7
6
  const set = new Set();
8
- const acc = [];
9
- let hash;
10
7
  for (const item of val) {
11
- hash = (0, fnv1A_1.default)(item);
12
- if (set.has(hash))
13
- continue;
14
- set.add(hash);
15
- acc.push(item);
8
+ if (!set.has(item))
9
+ set.add(item);
16
10
  }
17
- return acc;
11
+ return [...set];
18
12
  }
19
13
  exports.default = dedupe;
package/array/join.d.ts CHANGED
@@ -27,8 +27,8 @@ interface joinOptions {
27
27
  /**
28
28
  * Join an array of values while autofiltering any non-string/non-number elements
29
29
  *
30
- * @param val - Array of values to join
31
- * @param opts - Join options
30
+ * @param {unknown[]} val - Array of values to join
31
+ * @param {joinOptions?} opts - Join options
32
32
  *
33
33
  * @returns Joined array as string
34
34
  */
package/array/join.js CHANGED
@@ -1,24 +1,32 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const round_1 = require("../number/round");
4
- function join(val, opts = {}) {
5
- if (!Array.isArray(val) || val.length === 0)
4
+ function join(val, opts) {
5
+ if (!Array.isArray(val) || !val.length)
6
6
  return '';
7
- const OPTS = {
8
- delim: ' ',
9
- trim: true,
10
- valtrim: true,
11
- ...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
12
- };
7
+ let DELIM = ' ';
8
+ let TRIM = true;
9
+ let VALTRIM = true;
10
+ let VALROUND = false;
11
+ if (opts && Object.prototype.toString.call(opts) === '[object Object]') {
12
+ if (typeof opts.delim === 'string')
13
+ DELIM = opts.delim;
14
+ if (opts.trim === false)
15
+ TRIM = opts.trim;
16
+ if (opts.valtrim === false)
17
+ VALTRIM = opts.valtrim;
18
+ if (Number.isInteger(opts.valround) && opts.valround >= 0)
19
+ VALROUND = opts.valround;
20
+ }
13
21
  const filtered = [];
14
22
  for (const el of val) {
15
- if (typeof el === 'string' && el.trim().length > 0) {
16
- filtered.push(OPTS.valtrim === true ? el.trim() : el);
23
+ if (typeof el === 'string' && el.trim().length) {
24
+ filtered.push(VALTRIM ? el.trim() : el);
17
25
  }
18
26
  else if (Number.isFinite(el)) {
19
- filtered.push(Number.isFinite(OPTS.valround) ? (0, round_1.default)(el, OPTS.valround) : el);
27
+ filtered.push(VALROUND !== false ? (0, round_1.default)(el, VALROUND) : el);
20
28
  }
21
29
  }
22
- return OPTS.trim === true ? filtered.join(OPTS.delim).trim() : filtered.join(OPTS.delim);
30
+ return TRIM ? filtered.join(DELIM).trim() : filtered.join(DELIM);
23
31
  }
24
32
  exports.default = join;
package/array/mapFn.d.ts CHANGED
@@ -12,11 +12,10 @@ interface mapOptions {
12
12
  */
13
13
  merge?: boolean;
14
14
  }
15
- interface kvMap {
16
- [key: string]: {
17
- [key: string]: any;
18
- };
19
- }
15
+ type mapFn = (entry: {
16
+ [key: string]: any;
17
+ }) => (string | number | boolean);
18
+ type mapReturn = Record<string, Record<string, any>>;
20
19
  /**
21
20
  * Map an object array into a kv-object through a function that generates a key. Returning a non-string,
22
21
  * non-numeric value from the function (eg: false) will filter out the object.
@@ -26,15 +25,11 @@ interface kvMap {
26
25
  * Output:
27
26
  * {12: {uid: 12, name: 'Peter'}, 15: {uid: 15, name: 'Jonas'}}
28
27
  *
29
- * @param val - Array to map
30
- * @param fn - Handler function which is run for each of the objects and should return a string or number
31
- * @param opts - Options object to override built-in defaults
28
+ * @param {Record<string, any>[]} val - Array to map
29
+ * @param {mapFn} fn - Handler function which is run for each of the objects and should return a string or number
30
+ * @param {mapOptions?} opts - Options object to override built-in defaults
32
31
  *
33
- * @returns KV-Map object
32
+ * @returns {mapReturn} KV-Map object
34
33
  */
35
- export default function mapFn(arr: {
36
- [key: string]: any;
37
- }[], fn: (entry: {
38
- [key: string]: any;
39
- }) => (string | number | boolean), opts?: mapOptions): kvMap;
34
+ export default function mapFn(arr: Record<string, any>[], fn: mapFn, opts?: mapOptions): mapReturn;
40
35
  export {};
package/array/mapFn.js CHANGED
@@ -1,28 +1,24 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- function mapFn(arr, fn, opts = {}) {
4
- if ((!Array.isArray(arr) || arr.length === 0) ||
3
+ function mapFn(arr, fn, opts) {
4
+ if ((!Array.isArray(arr) || !arr.length) ||
5
5
  typeof fn !== 'function')
6
6
  return {};
7
- const OPTS = {
8
- merge: false,
9
- ...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
10
- };
7
+ let MERGE = false;
8
+ if (opts && Object.prototype.toString.call(opts) === '[object Object]') {
9
+ if (opts.merge === true)
10
+ MERGE = true;
11
+ }
11
12
  const map = {};
12
13
  let hash = false;
13
14
  for (const el of arr) {
14
15
  if (Object.prototype.toString.call(el) !== '[object Object]')
15
16
  continue;
16
17
  hash = fn(el);
17
- if (!Number.isFinite(hash) && !(typeof hash === 'string' && hash.trim().length > 0))
18
+ if (!Number.isFinite(hash) && !(typeof hash === 'string' && hash.trim().length))
18
19
  continue;
19
20
  hash = `${hash}`;
20
- if (OPTS.merge === true && map.hasOwnProperty(hash)) {
21
- map[hash] = { ...map[hash], ...el };
22
- }
23
- else {
24
- map[hash] = el;
25
- }
21
+ map[hash] = MERGE && map.hasOwnProperty(hash) ? { ...map[hash], ...el } : el;
26
22
  }
27
23
  return map;
28
24
  }
package/array/mapKey.d.ts CHANGED
@@ -12,11 +12,7 @@ interface mapOptions {
12
12
  */
13
13
  merge?: boolean;
14
14
  }
15
- interface kvMap {
16
- [key: string]: {
17
- [key: string]: any;
18
- };
19
- }
15
+ type kvMap = Record<string, Record<string, any>>;
20
16
  /**
21
17
  * Map an object array into a kv-object by passing a common key that exists on the objects. Objects for
22
18
  * which the key doesn't exist will be filtered out automatically
@@ -26,13 +22,11 @@ interface kvMap {
26
22
  * Output:
27
23
  * {12: {uid: 12, name: 'Peter'}, 15: {uid: 15, name: 'Jonas'}}
28
24
  *
29
- * @param val - Array to map
30
- * @param key - Key to map by
31
- * @param opts - Options object to override built-in defaults
25
+ * @param {Record<string,any>[]} val - Array to map
26
+ * @param {string} key - Key to map by
27
+ * @param {mapOptions?} opts - Options object to override built-in defaults
32
28
  *
33
- * @returns KV-Map object
29
+ * @returns {kvMap} KV-Map object
34
30
  */
35
- export default function mapKey(arr: {
36
- [key: string]: any;
37
- }[], key: string, opts?: mapOptions): kvMap;
31
+ export default function mapKey(arr: Record<string, any>[], key: string, opts?: mapOptions): kvMap;
38
32
  export {};
package/array/mapKey.js CHANGED
@@ -1,27 +1,23 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- function mapKey(arr, key, opts = {}) {
4
- if ((!Array.isArray(arr) || arr.length === 0) ||
3
+ function mapKey(arr, key, opts) {
4
+ if ((!Array.isArray(arr) || !arr.length) ||
5
5
  typeof key !== 'string')
6
6
  return {};
7
7
  const key_s = key.trim();
8
- if (key_s.length === 0)
8
+ if (!key_s.length)
9
9
  return {};
10
- const OPTS = {
11
- merge: false,
12
- ...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
13
- };
10
+ let MERGE = false;
11
+ if (opts && Object.prototype.toString.call(opts) === '[object Object]') {
12
+ if (opts.merge === true)
13
+ MERGE = true;
14
+ }
14
15
  const map = {};
15
16
  for (const el of arr) {
16
17
  if (Object.prototype.toString.call(el) !== '[object Object]' ||
17
18
  !Object.prototype.hasOwnProperty.call(el, key_s))
18
19
  continue;
19
- if (OPTS.merge === true && map.hasOwnProperty(el[key_s])) {
20
- map[el[key_s]] = { ...map[el[key_s]], ...el };
21
- }
22
- else {
23
- map[el[key_s]] = el;
24
- }
20
+ map[el[key_s]] = MERGE && map.hasOwnProperty(el[key_s]) ? { ...map[el[key_s]], ...el } : el;
25
21
  }
26
22
  return map;
27
23
  }
@@ -19,9 +19,7 @@ interface mapOptions {
19
19
  */
20
20
  keyround?: boolean;
21
21
  }
22
- interface mapReturn {
23
- [key: string]: string | number;
24
- }
22
+ type mapReturn = Record<string, string | number>;
25
23
  /**
26
24
  * Map an array of primitive values (numbers/strings) into a kv-object
27
25
  * non-numeric and non-string values will be filtered out
@@ -31,10 +29,10 @@ interface mapReturn {
31
29
  * Output:
32
30
  * {hello: 'hello', foo: 'foo', bar: 'bar'}
33
31
  *
34
- * @param val - Array to map
35
- * @param opts - Options object to override built-in defaults
32
+ * @param {unknown[]} val - Array to map
33
+ * @param {mapOptions?} opts - Options object to override built-in defaults
36
34
  *
37
- * @returns KV-Map object
35
+ * @returns {mapReturn} KV-Map object
38
36
  */
39
37
  export default function mapPrimitive(arr: unknown[], opts?: mapOptions): mapReturn;
40
38
  export {};
@@ -1,26 +1,35 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const round_1 = require("../number/round");
4
- function mapPrimitive(arr, opts = {}) {
5
- if (!Array.isArray(arr) || arr.length === 0)
4
+ function mapPrimitive(arr, opts) {
5
+ if (!Array.isArray(arr) || !arr.length)
6
6
  return {};
7
- const OPTS = {
8
- valtrim: false,
9
- valround: false,
10
- keyround: false,
11
- ...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
12
- };
7
+ let VALTRIM = false;
8
+ let VALROUND = false;
9
+ let KEYROUND = false;
10
+ if (opts && Object.prototype.toString.call(opts) === '[object Object]') {
11
+ if (opts.valtrim === true)
12
+ VALTRIM = true;
13
+ if (opts.valround === true ||
14
+ (Number.isInteger(opts.valround) && opts.valround >= 0))
15
+ VALROUND = opts.valround;
16
+ if (opts.keyround === true)
17
+ KEYROUND = true;
18
+ }
13
19
  const map = {};
14
20
  for (const el of arr) {
15
- if (typeof el === 'string' && el.trim().length > 0) {
16
- map[el.trim()] = OPTS.valtrim ? el.trim() : el;
21
+ if (typeof el === 'string') {
22
+ const trimmed = el.trim();
23
+ if (!trimmed.length)
24
+ continue;
25
+ map[trimmed] = VALTRIM ? trimmed : el;
17
26
  }
18
27
  else if (typeof el === 'number' && Number.isFinite(el)) {
19
- map[`${OPTS.keyround === true ? Math.round(el) : el}`] = OPTS.valround === false
28
+ map[`${KEYROUND ? Math.round(el) : el}`] = VALROUND === false
20
29
  ? el
21
- : OPTS.valround === true
30
+ : VALROUND === true
22
31
  ? Math.round(el)
23
- : (0, round_1.default)(el, OPTS.valround);
32
+ : (0, round_1.default)(el, VALROUND);
24
33
  }
25
34
  }
26
35
  return map;
package/array/shuffle.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function shuffle(arr) {
4
- if (!Array.isArray(arr) || arr.length === 0)
4
+ if (!Array.isArray(arr))
5
5
  return;
6
6
  for (let i = arr.length - 1; i > 0; i--) {
7
7
  const j = Math.floor(Math.random() * (i + 1));
package/array/sort.js CHANGED
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const is_1 = require("../boolean/is");
4
3
  const isNotEmpty_1 = require("../object/isNotEmpty");
5
4
  function partition(arr, start_ix, end_ix) {
6
5
  const pivot_val = arr[Math.floor((start_ix + end_ix) / 2)].t;
@@ -29,27 +28,32 @@ function quickSort(arr, start_ix = 0, end_ix = arr.length - 1) {
29
28
  }
30
29
  return arr;
31
30
  }
32
- function sort(arr, by, dir = 'asc', opts = {}) {
33
- if (!Array.isArray(arr) || arr.length === 0)
31
+ function sort(arr, by, dir = 'asc', opts) {
32
+ if (!Array.isArray(arr) || !arr.length)
34
33
  return [];
35
34
  if (dir !== 'asc' && dir !== 'desc')
36
35
  throw new Error('Direction should be either asc or desc');
37
- const has_opts = Object.prototype.toString.call(opts) === '[object Object]';
38
- const OPTS = {
39
- filter_fn: has_opts && typeof opts.filter_fn === 'function'
40
- ? el => (0, isNotEmpty_1.default)(el) && opts.filter_fn(el)
41
- : isNotEmpty_1.default,
42
- nokey_hide: has_opts && (0, is_1.default)(opts.nokey_hide) ? opts.nokey_hide : false,
43
- nokey_atend: has_opts && (0, is_1.default)(opts.nokey_atend) ? opts.nokey_atend : true,
44
- };
36
+ let NOKEY_HIDE = false;
37
+ let NOKEY_AT_END = true;
38
+ let FILTER_FN = isNotEmpty_1.default;
39
+ if (opts && Object.prototype.toString.call(opts) === '[object Object]') {
40
+ if (opts.nokey_hide === true)
41
+ NOKEY_HIDE = true;
42
+ if (opts.nokey_atend === false)
43
+ NOKEY_AT_END = false;
44
+ if (typeof opts.filter_fn === 'function') {
45
+ const fn = opts.filter_fn;
46
+ FILTER_FN = el => (0, isNotEmpty_1.default)(el) && fn(el);
47
+ }
48
+ }
45
49
  const prepared_arr = [];
46
50
  const nokey_arr = [];
47
51
  if (typeof by === 'string') {
48
52
  const by_s = by.trim();
49
- if (by_s.length === 0)
53
+ if (!by_s.length)
50
54
  throw new Error('Sort by as string should contain content');
51
55
  for (const el of arr) {
52
- if (!OPTS.filter_fn(el))
56
+ if (!FILTER_FN(el))
53
57
  continue;
54
58
  if (!Object.prototype.hasOwnProperty.call(el, by_s) || el[by_s] === undefined) {
55
59
  nokey_arr.push(el);
@@ -62,7 +66,7 @@ function sort(arr, by, dir = 'asc', opts = {}) {
62
66
  else if (typeof by === 'function') {
63
67
  let key;
64
68
  for (const el of arr) {
65
- if (!OPTS.filter_fn(el))
69
+ if (!FILTER_FN(el))
66
70
  continue;
67
71
  key = by(el);
68
72
  if (key === undefined) {
@@ -80,11 +84,11 @@ function sort(arr, by, dir = 'asc', opts = {}) {
80
84
  if (dir === 'desc')
81
85
  prepared_arr.reverse();
82
86
  const result = [];
83
- if (OPTS.nokey_hide) {
87
+ if (NOKEY_HIDE) {
84
88
  for (const obj of prepared_arr)
85
89
  result.push(obj.el);
86
90
  }
87
- else if (OPTS.nokey_atend) {
91
+ else if (NOKEY_AT_END) {
88
92
  for (const obj of prepared_arr)
89
93
  result.push(obj.el);
90
94
  for (const el of nokey_arr)
package/date/addUTC.d.ts CHANGED
@@ -7,4 +7,4 @@
7
7
  *
8
8
  * @returns New date with provided amount of key added
9
9
  */
10
- export default function addUTC(val: Date, amount?: number, key?: 'years' | 'year' | 'months' | 'month' | 'days' | 'day' | 'hours' | 'hour' | 'minutes' | 'minute' | 'seconds' | 'second' | 'milliseconds' | 'millisecond'): Date;
10
+ export default function addUTC(val: Date, amt?: number, key?: 'years' | 'year' | 'months' | 'month' | 'days' | 'day' | 'hours' | 'hour' | 'minutes' | 'minute' | 'seconds' | 'second' | 'milliseconds' | 'millisecond'): Date;
package/date/addUTC.js CHANGED
@@ -1,52 +1,44 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const is_1 = require("./is");
4
- function addUTC(val, amount = 0, key = 'millisecond') {
4
+ function addUTC(val, amt = 0, key = 'millisecond') {
5
5
  if (!(0, is_1.default)(val))
6
6
  throw new TypeError('addUTC requires a date object');
7
- if (!Number.isInteger(amount))
7
+ if (!Number.isInteger(amt))
8
8
  throw new TypeError('Amount needs to be an integer');
9
9
  if (typeof key !== 'string')
10
10
  throw new TypeError('Key needs to be a string with content');
11
- const copy = new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds()));
11
+ const year = val.getUTCFullYear();
12
+ const month = val.getUTCMonth();
13
+ const date = val.getUTCDate();
14
+ const hour = val.getUTCHours();
15
+ const min = val.getUTCMinutes();
16
+ const sec = val.getUTCSeconds();
17
+ const ms = val.getUTCMilliseconds();
12
18
  switch (key) {
13
19
  case 'years':
14
- case 'year': {
15
- copy.setUTCFullYear(copy.getUTCFullYear() + amount);
16
- return copy;
17
- }
20
+ case 'year':
21
+ return new Date(Date.UTC(year + amt, month, date, hour, min, sec, ms));
18
22
  case 'months':
19
- case 'month': {
20
- copy.setUTCMonth(copy.getUTCMonth() + amount);
21
- return copy;
22
- }
23
+ case 'month':
24
+ return new Date(Date.UTC(year, month + amt, date, hour, min, sec, ms));
23
25
  case 'days':
24
- case 'day': {
25
- copy.setUTCDate(copy.getUTCDate() + amount);
26
- return copy;
27
- }
26
+ case 'day':
27
+ return new Date(Date.UTC(year, month, date + amt, hour, min, sec, ms));
28
28
  case 'hours':
29
- case 'hour': {
30
- copy.setUTCHours(copy.getUTCHours() + amount);
31
- return copy;
32
- }
29
+ case 'hour':
30
+ return new Date(Date.UTC(year, month, date, hour + amt, min, sec, ms));
33
31
  case 'minutes':
34
- case 'minute': {
35
- copy.setUTCMinutes(copy.getUTCMinutes() + amount);
36
- return copy;
37
- }
32
+ case 'minute':
33
+ return new Date(Date.UTC(year, month, date, hour, min + amt, sec, ms));
38
34
  case 'seconds':
39
- case 'second': {
40
- copy.setUTCSeconds(copy.getUTCSeconds() + amount);
41
- return copy;
42
- }
35
+ case 'second':
36
+ return new Date(Date.UTC(year, month, date, hour, min, sec + amt, ms));
43
37
  case 'milliseconds':
44
- case 'millisecond': {
45
- copy.setUTCMilliseconds(copy.getUTCMilliseconds() + amount);
46
- return copy;
47
- }
38
+ case 'millisecond':
39
+ return new Date(Date.UTC(year, month, date, hour, min, sec, ms + amt));
48
40
  default:
49
- return copy;
41
+ return new Date(Date.UTC(year, month, date, hour, min, sec, ms));
50
42
  }
51
43
  }
52
44
  exports.default = addUTC;
package/date/diff.js CHANGED
@@ -29,8 +29,6 @@ function diff(val_a, val_b, key = 'millisecond') {
29
29
  case 'second':
30
30
  case 'seconds':
31
31
  return diff_in_ms / SECOND_IN_MILLISECONDS;
32
- case 'millisecond':
33
- case 'milliseconds':
34
32
  default:
35
33
  return diff_in_ms;
36
34
  }
package/date/endOfUTC.js CHANGED
@@ -1,16 +1,16 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const is_1 = require("./is");
4
- const WEEK_END = {
5
- week: 0,
6
- week_sun: 6,
7
- week_mon: 0,
8
- week_tue: 1,
9
- week_wed: 2,
10
- week_thu: 3,
11
- week_fri: 4,
12
- week_sat: 5,
13
- };
4
+ const WEEK_END = new Map([
5
+ ['week', 0],
6
+ ['week_sun', 6],
7
+ ['week_mon', 0],
8
+ ['week_tue', 1],
9
+ ['week_wed', 2],
10
+ ['week_thu', 3],
11
+ ['week_fri', 4],
12
+ ['week_sat', 5],
13
+ ]);
14
14
  function endOfUTC(val, key = 'millisecond') {
15
15
  if (!(0, is_1.default)(val))
16
16
  throw new TypeError('endOfUTC requires a date object');
@@ -20,7 +20,8 @@ function endOfUTC(val, key = 'millisecond') {
20
20
  case 'year':
21
21
  return new Date(Date.UTC(val.getUTCFullYear(), 11, 31, 23, 59, 59, 999));
22
22
  case 'quarter': {
23
- return new Date(Date.UTC(val.getUTCFullYear(), (val.getUTCMonth() - (val.getUTCMonth() % 3)) + 3, 0, 23, 59, 59, 999));
23
+ const UTC_MONTH = val.getUTCMonth();
24
+ return new Date(Date.UTC(val.getUTCFullYear(), (UTC_MONTH - (UTC_MONTH % 3)) + 3, 0, 23, 59, 59, 999));
24
25
  }
25
26
  case 'month':
26
27
  return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth() + 1, 0, 23, 59, 59, 999));
@@ -33,7 +34,7 @@ function endOfUTC(val, key = 'millisecond') {
33
34
  case 'week_fri':
34
35
  case 'week_sat': {
35
36
  const UTC_DAY = val.getUTCDay();
36
- const UTC_EOD = WEEK_END[key];
37
+ const UTC_EOD = WEEK_END.get(key);
37
38
  return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate() + (UTC_DAY <= UTC_EOD ? UTC_EOD - UTC_DAY : (7 - UTC_DAY) + UTC_EOD), 23, 59, 59, 999));
38
39
  }
39
40
  case 'day':
@@ -44,7 +45,6 @@ function endOfUTC(val, key = 'millisecond') {
44
45
  return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), 59, 999));
45
46
  case 'second':
46
47
  return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), 999));
47
- case 'millisecond':
48
48
  default:
49
49
  return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds()));
50
50
  }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Formats the provided date according to a specific spec
3
+ *
4
+ * @param {Date} val - Date to format
5
+ * @param {string} spec - Spec to format the date to
6
+ * @param {string} locale - Locale to format the date in (only used in certain tokens such as dddd and MMMM)
7
+ * @param {string} zone - (default=current timezone) Pass the timezone to convert into. If not passed no conversion will happen
8
+ * @returns {string} Formatted date as string
9
+ * @throws {TypeError} When provided invalid payload
10
+ */
11
+ export default function format(val: Date, spec: string, locale?: string, zone?: string): string;