@valkyriestudios/utils 12.22.0 → 12.24.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
@@ -787,7 +787,7 @@ isFormData(new FormData()); // TRUE
787
787
  isFormData({hi: 'there'}); // FALSE
788
788
  ```
789
789
 
790
- ### formdata/toObject(val:FormData, {raw?:string[]} = {})
790
+ ### formdata/toObject(val:FormData, {raw?:string[]|true;single?:string[]} = {})
791
791
  Converts an instance of FormData to an object
792
792
  ```typescript
793
793
  import toObject from '@valkyriestudios/utils/formdata/toObject';
@@ -840,6 +840,19 @@ toObject(form, {raw: ['pincode']}); /* {
840
840
  } */
841
841
  ```
842
842
 
843
+ Take Note: Set raw to `true` to do no normalization
844
+
845
+ Allows passing a 'single' list that tells the system to NEVER turn a particular value into an array of values:
846
+ ```typescript
847
+ const formData = new FormData();
848
+ formData.append('status', 'active');
849
+ formData.append('status', 'inactive');
850
+ formData.append('action', 'save');
851
+ formData.append('action', 'reset');
852
+
853
+ toObject(formData, { single: ['status', 'action'] }) /* {status: 'inactive', action: 'reset'} */
854
+ ```
855
+
843
856
  ### hash/guid()
844
857
  Generate a unique identifier (guid) according to RFC4122
845
858
  ```typescript
@@ -1239,4 +1252,4 @@ humanizeNumber(47328748923747923479); // '47,328.75q'
1239
1252
 
1240
1253
  ## Contributors
1241
1254
  - [Peter Vermeulen](https://www.linkedin.com/in/petervermeulen1/)
1242
- - [Xander Berkein](https://github.com/xanderberkein)
1255
+ - [Xander Berkein](https://github.com/xanderberkein)
package/array/join.js CHANGED
@@ -4,7 +4,7 @@ exports.join = join;
4
4
  exports.default = join;
5
5
  const round_1 = require("../number/round");
6
6
  const isIntegerAboveOrEqual_1 = require("../number/isIntegerAboveOrEqual");
7
- const SPACE_RGX = /(\s){2,}/g;
7
+ const SPACE_RGX = /(\s)+/g;
8
8
  function join(val, opts) {
9
9
  if (!Array.isArray(val) || !val.length)
10
10
  return '';
@@ -17,9 +17,12 @@ function join(val, opts) {
17
17
  let hasVal = false;
18
18
  for (let i = 0; i < val.length; i++) {
19
19
  const el = val[i];
20
- if (typeof el === 'string' && el.trim().length) {
21
- const trimmed = VALTRIM ? el.trim() : el;
22
- result = result + (hasVal ? DELIM : '') + (INNERTRIM ? trimmed.replace(SPACE_RGX, ' ') : trimmed);
20
+ if (typeof el === 'string') {
21
+ const trimmed = el.trim();
22
+ if (!trimmed)
23
+ continue;
24
+ const n_el = VALTRIM ? trimmed : el;
25
+ result = result + (hasVal ? DELIM : '') + (INNERTRIM ? n_el.replace(SPACE_RGX, ' ') : n_el);
23
26
  hasVal = true;
24
27
  }
25
28
  else if (Number.isFinite(el)) {
@@ -17,11 +17,11 @@ function mapPrimitive(arr, opts = {}) {
17
17
  const el = arr[i];
18
18
  if (typeof el === 'string') {
19
19
  const trimmed = el.trim();
20
- if (!trimmed.length)
20
+ if (!trimmed)
21
21
  continue;
22
22
  map[trimmed] = VALTRIM ? trimmed : el;
23
23
  }
24
- else if (typeof el === 'number' && Number.isFinite(el)) {
24
+ else if (Number.isFinite(el)) {
25
25
  map[`${KEYROUND ? Math.round(el) : el}`] = VALROUND === false
26
26
  ? el
27
27
  : VALROUND === true
package/date/addUTC.d.ts CHANGED
@@ -2,9 +2,9 @@ export type AddUTCKey = 'years' | 'year' | 'months' | 'month' | 'days' | 'day' |
2
2
  /**
3
3
  * Adds the provided amount of a specific key to the provided date
4
4
  *
5
- * @param {Date} val - Date to set to end of
5
+ * @param {Date|string} val - Date to set to end of
6
6
  * @param {number} amount - (default=0) Amount of key to add
7
7
  * @param {AddUTCKey} key - (default='millisecond') Key to set
8
8
  */
9
- declare function addUTC(val: Date, amt?: number, key?: AddUTCKey): Date;
9
+ declare function addUTC(val: Date | string, amt?: number, key?: AddUTCKey): Date;
10
10
  export { addUTC, addUTC as default };
package/date/addUTC.js CHANGED
@@ -2,19 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.addUTC = addUTC;
4
4
  exports.default = addUTC;
5
- const is_1 = require("./is");
5
+ const convertToDate_1 = require("./convertToDate");
6
6
  function addUTC(val, amt = 0, key = 'millisecond') {
7
- if (!(0, is_1.isDate)(val))
8
- throw new TypeError('addUTC requires a date object');
9
7
  if (!Number.isInteger(amt))
10
8
  throw new TypeError('Amount needs to be an integer');
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();
9
+ const n_val = (0, convertToDate_1.convertToDate)(val);
10
+ if (n_val === null)
11
+ throw new TypeError('addUTC requires a date object');
12
+ const year = n_val.getUTCFullYear();
13
+ const month = n_val.getUTCMonth();
14
+ const date = n_val.getUTCDate();
15
+ const hour = n_val.getUTCHours();
16
+ const min = n_val.getUTCMinutes();
17
+ const sec = n_val.getUTCSeconds();
18
+ const ms = n_val.getUTCMilliseconds();
18
19
  switch (key) {
19
20
  case 'years':
20
21
  case 'year':
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Convert a value to a Date object if possible, handles Date objects and strings
3
+ *
4
+ * @param {Date|string} val - Value to convert
5
+ */
6
+ declare function convertToDate(val: Date | string | number): Date | null;
7
+ export { convertToDate, convertToDate as default };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertToDate = convertToDate;
4
+ exports.default = convertToDate;
5
+ const is_1 = require("./is");
6
+ function convertToDate(val) {
7
+ if ((0, is_1.isDate)(val)) {
8
+ return val;
9
+ }
10
+ else if (typeof val === 'string') {
11
+ const date = new Date(val);
12
+ return Number.isNaN(date.getTime()) ? null : date;
13
+ }
14
+ return null;
15
+ }
package/date/diff.d.ts CHANGED
@@ -2,9 +2,9 @@ export type DiffKey = 'week' | 'weeks' | 'day' | 'days' | 'hour' | 'hours' | 'mi
2
2
  /**
3
3
  * Compute the diff between two dates in the provided key
4
4
  *
5
- * @param {Date} val_a - Date to diff against
6
- * @param {Date} val_b - Date to diff with
5
+ * @param {Date|string} val_a - Date to diff against
6
+ * @param {Date|string} val_b - Date to diff with
7
7
  * @param {DiffKey} key - (default='millisecond') Key to diff in
8
8
  */
9
- declare function diff(val_a: Date, val_b: Date, key?: DiffKey): number;
9
+ declare function diff(val_a: Date | string, val_b: Date | string, key?: DiffKey): number;
10
10
  export { diff, diff as default };
package/date/diff.js CHANGED
@@ -2,17 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.diff = diff;
4
4
  exports.default = diff;
5
- const is_1 = require("./is");
5
+ const convertToDate_1 = require("./convertToDate");
6
6
  const SECOND_IN_MILLISECONDS = 1000;
7
7
  const MINUTE_IN_MILLISECONDS = SECOND_IN_MILLISECONDS * 60;
8
8
  const HOUR_IN_MILLISECONDS = MINUTE_IN_MILLISECONDS * 60;
9
9
  const DAY_IN_MILLISECONDS = HOUR_IN_MILLISECONDS * 24;
10
10
  const WEEK_IN_MILLISECONDS = DAY_IN_MILLISECONDS * 7;
11
11
  function diff(val_a, val_b, key = 'millisecond') {
12
- if (!(0, is_1.isDate)(val_a) ||
13
- !(0, is_1.isDate)(val_b))
12
+ const n_val_a = (0, convertToDate_1.convertToDate)(val_a);
13
+ const n_val_b = (0, convertToDate_1.convertToDate)(val_b);
14
+ if (n_val_a === null ||
15
+ n_val_b === null)
14
16
  throw new TypeError('Diff requires date objects for both values');
15
- const diff_in_ms = val_a.valueOf() - val_b.valueOf();
17
+ const diff_in_ms = n_val_a.valueOf() - n_val_b.valueOf();
16
18
  switch (key) {
17
19
  case 'week':
18
20
  case 'weeks':
@@ -2,8 +2,8 @@ export type EndOfUTCKey = 'year' | 'quarter' | 'month' | 'week' | 'week_sun' | '
2
2
  /**
3
3
  * Sets the provided date to end of UTC of provided key
4
4
  *
5
- * @param {Date} val - Date to set to end of
5
+ * @param {Date|string} val - Date to set to end of
6
6
  * @param {EndOfUTCKey} key - (default='millisecond') Key to set
7
7
  */
8
- declare function endOfUTC(val: Date, key?: EndOfUTCKey): Date;
8
+ declare function endOfUTC(val: Date | string, key?: EndOfUTCKey): Date;
9
9
  export { endOfUTC, endOfUTC as default };
package/date/endOfUTC.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.endOfUTC = endOfUTC;
4
4
  exports.default = endOfUTC;
5
- const is_1 = require("./is");
5
+ const convertToDate_1 = require("./convertToDate");
6
6
  const WEEK_END = new Map([
7
7
  ['week', 0],
8
8
  ['week_sun', 6],
@@ -14,17 +14,19 @@ const WEEK_END = new Map([
14
14
  ['week_sat', 5],
15
15
  ]);
16
16
  function endOfUTC(val, key = 'millisecond') {
17
- if (!(0, is_1.isDate)(val))
17
+ const n_val = (0, convertToDate_1.convertToDate)(val);
18
+ if (n_val === null)
18
19
  throw new TypeError('endOfUTC requires a date object');
20
+ const year = n_val.getUTCFullYear();
19
21
  switch (key) {
20
22
  case 'year':
21
- return new Date(Date.UTC(val.getUTCFullYear(), 11, 31, 23, 59, 59, 999));
23
+ return new Date(Date.UTC(year, 11, 31, 23, 59, 59, 999));
22
24
  case 'quarter': {
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));
25
+ const UTC_MONTH = n_val.getUTCMonth();
26
+ return new Date(Date.UTC(year, (UTC_MONTH - (UTC_MONTH % 3)) + 3, 0, 23, 59, 59, 999));
25
27
  }
26
28
  case 'month':
27
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth() + 1, 0, 23, 59, 59, 999));
29
+ return new Date(Date.UTC(year, n_val.getUTCMonth() + 1, 0, 23, 59, 59, 999));
28
30
  case 'week':
29
31
  case 'week_sun':
30
32
  case 'week_mon':
@@ -33,19 +35,19 @@ function endOfUTC(val, key = 'millisecond') {
33
35
  case 'week_thu':
34
36
  case 'week_fri':
35
37
  case 'week_sat': {
36
- const UTC_DAY = val.getUTCDay();
38
+ const UTC_DAY = n_val.getUTCDay();
37
39
  const UTC_EOD = WEEK_END.get(key);
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));
40
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate() + (UTC_DAY <= UTC_EOD ? UTC_EOD - UTC_DAY : (7 - UTC_DAY) + UTC_EOD), 23, 59, 59, 999));
39
41
  }
40
42
  case 'day':
41
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), 23, 59, 59, 999));
43
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate(), 23, 59, 59, 999));
42
44
  case 'hour':
43
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), 59, 59, 999));
45
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate(), n_val.getUTCHours(), 59, 59, 999));
44
46
  case 'minute':
45
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), 59, 999));
47
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate(), n_val.getUTCHours(), n_val.getUTCMinutes(), 59, 999));
46
48
  case 'second':
47
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), 999));
49
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate(), n_val.getUTCHours(), n_val.getUTCMinutes(), n_val.getUTCSeconds(), 999));
48
50
  default:
49
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds()));
51
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate(), n_val.getUTCHours(), n_val.getUTCMinutes(), n_val.getUTCSeconds(), n_val.getUTCMilliseconds()));
50
52
  }
51
53
  }
package/date/format.d.ts CHANGED
@@ -7,14 +7,14 @@ export type WEEK_START = keyof typeof WEEK_STARTS;
7
7
  /**
8
8
  * Formats the provided date according to a specific spec
9
9
  *
10
- * @param {Date} val - Date to format
10
+ * @param {Date|string} val - Date to format
11
11
  * @param {string} spec - Spec to format the date to
12
12
  * @param {string} locale - Locale to format the date in (only used in certain tokens such as dddd and MMMM)
13
13
  * @param {string} zone - (default=current timezone) Pass the timezone to convert into. If not passed no conversion will happen
14
14
  * @param {string} sow - (default='mon') Start of week (only useful when working with the 'W' and 'w' tokens for week numbers
15
15
  * @throws {TypeError} When provided invalid payload
16
16
  */
17
- declare function format(val: Date, spec: string, locale?: string, zone?: string, sow?: WEEK_START): string;
17
+ declare function format(val: Date | string, spec: string, locale?: string, zone?: string, sow?: WEEK_START): string;
18
18
  declare namespace format {
19
19
  var getLocale: () => string;
20
20
  var setLocale: (locale: string) => void;
package/date/format.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.format = format;
4
4
  exports.default = format;
5
- const is_1 = require("./is");
5
+ const convertToDate_1 = require("./convertToDate");
6
6
  const WEEK_STARTS = {
7
7
  mon: 'mon',
8
8
  sun: 'sun',
@@ -14,7 +14,7 @@ let DEFAULT_SOW = 'mon';
14
14
  try {
15
15
  DEFAULT_TZ = Intl.DateTimeFormat().resolvedOptions().timeZone;
16
16
  }
17
- catch (err) {
17
+ catch {
18
18
  }
19
19
  finally {
20
20
  if (typeof DEFAULT_TZ !== 'string')
@@ -25,17 +25,17 @@ const intl_formatters = Object.create(null);
25
25
  const spec_cache = Object.create(null);
26
26
  const zone_offset_cache = Object.create(null);
27
27
  function WeekNr(d, sow) {
28
- const date = new Date(d.valueOf());
29
28
  switch (sow) {
30
29
  case 'sun':
31
30
  case 'sat': {
32
31
  const OFFSET = sow === 'sat' ? 1 : 0;
33
- const jan1 = new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
34
- const near = new Date(date.getTime() - (((date.getDay() + OFFSET) % 7) * 86400000));
32
+ const jan1 = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
33
+ const near = new Date(d.getTime() - (((d.getDay() + OFFSET) % 7) * 86400000));
35
34
  const first = new Date(jan1.getTime() - (((jan1.getDay() + OFFSET) % 7) * 86400000));
36
35
  return 1 + Math.floor((near.valueOf() - first.valueOf()) / 604800000);
37
36
  }
38
37
  default: {
38
+ const date = new Date(d.valueOf());
39
39
  date.setDate(date.getDate() - ((d.getDay() + 6) % 7) + 3);
40
40
  const date_thu = date.valueOf();
41
41
  date.setMonth(0, 1);
@@ -61,7 +61,7 @@ function toZone(d, zone) {
61
61
  try {
62
62
  zone_time = new Date(d.toLocaleString(DEFAULT_LOCALE, { timeZone: zone })).getTime();
63
63
  }
64
- catch (err) {
64
+ catch {
65
65
  throw new Error(`format: Invalid zone passed - ${zone}`);
66
66
  }
67
67
  const offset = zone_time - time;
@@ -76,7 +76,7 @@ function runIntl(loc, token, props, val) {
76
76
  formatter = new Intl.DateTimeFormat(loc, props);
77
77
  intl_formatters[hash] = formatter;
78
78
  }
79
- catch (err) {
79
+ catch {
80
80
  throw new Error(`format: Failed to run conversion for ${token} with locale ${loc}`);
81
81
  }
82
82
  }
@@ -176,7 +176,8 @@ function getSpecChain(spec) {
176
176
  return result;
177
177
  }
178
178
  function format(val, spec, locale = DEFAULT_LOCALE, zone = DEFAULT_TZ, sow = DEFAULT_SOW) {
179
- if (!(0, is_1.isDate)(val))
179
+ const n_val = (0, convertToDate_1.convertToDate)(val);
180
+ if (n_val === null)
180
181
  throw new TypeError('format: val must be a Date');
181
182
  if (typeof spec !== 'string')
182
183
  throw new TypeError('format: spec must be a string');
@@ -186,8 +187,8 @@ function format(val, spec, locale = DEFAULT_LOCALE, zone = DEFAULT_TZ, sow = DEF
186
187
  throw new TypeError('format: zone must be a string');
187
188
  const n_spec = getSpecChain(spec);
188
189
  if (!n_spec)
189
- return val.toISOString();
190
- const d = toZone(val, zone);
190
+ return n_val.toISOString();
191
+ const d = toZone(n_val, zone);
191
192
  let base = n_spec.base;
192
193
  const { chain_len, chain, repl_len, repl } = n_spec;
193
194
  for (let i = 0; i < chain_len; i++) {
package/date/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { addUTC } from './addUTC';
2
+ import { convertToDate } from './convertToDate';
2
3
  import { diff } from './diff';
3
4
  import { endOfUTC } from './endOfUTC';
4
5
  import { format } from './format';
@@ -11,4 +12,4 @@ import { setTimeUTC } from './setTimeUTC';
11
12
  import { startOfUTC } from './startOfUTC';
12
13
  import { toUnix } from './toUnix';
13
14
  import { toUTC } from './toUTC';
14
- export { addUTC, diff, endOfUTC, format, isDateFormat as isFormat, isDateFormat, isDate, isDate as is, isLeap, nowUnix, nowUnixMs, setTimeUTC, startOfUTC, toUnix, toUTC };
15
+ export { addUTC, convertToDate, diff, endOfUTC, format, isDateFormat as isFormat, isDateFormat, isDate, isDate as is, isLeap, nowUnix, nowUnixMs, setTimeUTC, startOfUTC, toUnix, toUTC };
package/date/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toUTC = exports.toUnix = exports.startOfUTC = exports.setTimeUTC = exports.nowUnixMs = exports.nowUnix = exports.isLeap = exports.is = exports.isDate = exports.isDateFormat = exports.isFormat = exports.format = exports.endOfUTC = exports.diff = exports.addUTC = void 0;
3
+ exports.toUTC = exports.toUnix = exports.startOfUTC = exports.setTimeUTC = exports.nowUnixMs = exports.nowUnix = exports.isLeap = exports.is = exports.isDate = exports.isDateFormat = exports.isFormat = exports.format = exports.endOfUTC = exports.diff = exports.convertToDate = exports.addUTC = void 0;
4
4
  const addUTC_1 = require("./addUTC");
5
5
  Object.defineProperty(exports, "addUTC", { enumerable: true, get: function () { return addUTC_1.addUTC; } });
6
+ const convertToDate_1 = require("./convertToDate");
7
+ Object.defineProperty(exports, "convertToDate", { enumerable: true, get: function () { return convertToDate_1.convertToDate; } });
6
8
  const diff_1 = require("./diff");
7
9
  Object.defineProperty(exports, "diff", { enumerable: true, get: function () { return diff_1.diff; } });
8
10
  const endOfUTC_1 = require("./endOfUTC");
package/date/isLeap.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Check whether or not a provided value is a leap year
3
3
  *
4
- * @param {Date} val - Value to verify
4
+ * @param {Date|string} val - Value to verify
5
5
  */
6
- declare function isLeap(val: Date): boolean;
6
+ declare function isLeap(val: Date | string): boolean;
7
7
  export { isLeap, isLeap as default };
package/date/isLeap.js CHANGED
@@ -2,10 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isLeap = isLeap;
4
4
  exports.default = isLeap;
5
- const is_1 = require("./is");
5
+ const convertToDate_1 = require("./convertToDate");
6
6
  function isLeap(val) {
7
- if (!(0, is_1.isDate)(val))
7
+ const n_val = (0, convertToDate_1.convertToDate)(val);
8
+ if (n_val === null)
8
9
  return false;
9
- const year = val.getUTCFullYear();
10
- return (((year % 4) === 0) && ((year % 100) !== 0)) || ((year % 400) === 0);
10
+ const year = n_val.getUTCFullYear();
11
+ return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
11
12
  }
@@ -7,8 +7,8 @@ export type TimeProps = {
7
7
  /**
8
8
  * Sets the time on a provided date object and returns it
9
9
  *
10
- * @param {Date} val - Date to set the time for
10
+ * @param {Date|string} val - Date to set the time for
11
11
  * @param {TimeProps} props - Time props to set the time to
12
12
  */
13
- declare function setTimeUTC(val: Date, props: TimeProps): Date;
13
+ declare function setTimeUTC(val: Date | string, props: TimeProps): Date;
14
14
  export { setTimeUTC, setTimeUTC as default };
@@ -2,10 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setTimeUTC = setTimeUTC;
4
4
  exports.default = setTimeUTC;
5
- const is_1 = require("./is");
5
+ const convertToDate_1 = require("./convertToDate");
6
6
  const isIntegerBetween_1 = require("../number/isIntegerBetween");
7
7
  function setTimeUTC(val, props) {
8
- if (!(0, is_1.isDate)(val))
8
+ const n_val = (0, convertToDate_1.convertToDate)(val);
9
+ if (n_val === null)
9
10
  throw new TypeError('setTimeUTC requires a date object');
10
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), (0, isIntegerBetween_1.isIntegerBetween)(props?.hour, 0, 23) ? props.hour : val.getUTCHours(), (0, isIntegerBetween_1.isIntegerBetween)(props?.minute, 0, 59) ? props?.minute : val.getUTCMinutes(), (0, isIntegerBetween_1.isIntegerBetween)(props?.second, 0, 59) ? props?.second : val.getUTCSeconds(), (0, isIntegerBetween_1.isIntegerBetween)(props?.millisecond, 0, 999) ? props?.millisecond : val.getUTCMilliseconds()));
11
+ const { hour, minute, second, millisecond } = props || {};
12
+ return new Date(Date.UTC(n_val.getUTCFullYear(), n_val.getUTCMonth(), n_val.getUTCDate(), (0, isIntegerBetween_1.isIntegerBetween)(hour, 0, 23) ? hour : n_val.getUTCHours(), (0, isIntegerBetween_1.isIntegerBetween)(minute, 0, 59) ? minute : n_val.getUTCMinutes(), (0, isIntegerBetween_1.isIntegerBetween)(second, 0, 59) ? second : n_val.getUTCSeconds(), (0, isIntegerBetween_1.isIntegerBetween)(millisecond, 0, 999) ? millisecond : n_val.getUTCMilliseconds()));
11
13
  }
@@ -2,8 +2,8 @@ export type StartOfUTCKey = 'year' | 'quarter' | 'month' | 'week' | 'week_sun' |
2
2
  /**
3
3
  * Sets the provided date to start of UTC of provided key
4
4
  *
5
- * @param {Date} val - Date to set to start of
5
+ * @param {Date|string} val - Date to set to start of
6
6
  * @param {StartOfUTCKey} key - (default='millisecond') Key to set
7
7
  */
8
- declare function startOfUTC(val: Date, key?: StartOfUTCKey): Date;
8
+ declare function startOfUTC(val: Date | string, key?: StartOfUTCKey): Date;
9
9
  export { startOfUTC, startOfUTC as default };
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.startOfUTC = startOfUTC;
4
4
  exports.default = startOfUTC;
5
- const is_1 = require("./is");
5
+ const convertToDate_1 = require("./convertToDate");
6
6
  const WEEK_START = new Map([
7
7
  ['week', 1],
8
8
  ['week_sun', 0],
@@ -14,17 +14,19 @@ const WEEK_START = new Map([
14
14
  ['week_sat', 6],
15
15
  ]);
16
16
  function startOfUTC(val, key = 'millisecond') {
17
- if (!(0, is_1.isDate)(val))
17
+ const n_val = (0, convertToDate_1.convertToDate)(val);
18
+ if (n_val === null)
18
19
  throw new TypeError('startOfUTC requires a date object');
20
+ const year = n_val.getUTCFullYear();
19
21
  switch (key) {
20
22
  case 'year':
21
- return new Date(Date.UTC(val.getUTCFullYear(), 0, 1, 0, 0, 0, 0));
23
+ return new Date(Date.UTC(year, 0));
22
24
  case 'quarter': {
23
- const UTC_MONTH = val.getUTCMonth();
24
- return new Date(Date.UTC(val.getUTCFullYear(), UTC_MONTH - (UTC_MONTH % 3), 1, 0, 0, 0, 0));
25
+ const UTC_MONTH = n_val.getUTCMonth();
26
+ return new Date(Date.UTC(year, UTC_MONTH - (UTC_MONTH % 3)));
25
27
  }
26
28
  case 'month':
27
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), 1, 0, 0, 0, 0));
29
+ return new Date(Date.UTC(year, n_val.getUTCMonth()));
28
30
  case 'week':
29
31
  case 'week_sun':
30
32
  case 'week_mon':
@@ -33,19 +35,19 @@ function startOfUTC(val, key = 'millisecond') {
33
35
  case 'week_thu':
34
36
  case 'week_fri':
35
37
  case 'week_sat': {
36
- const UTC_DAY = val.getUTCDay();
38
+ const UTC_DAY = n_val.getUTCDay();
37
39
  const UTC_SOD = WEEK_START.get(key);
38
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate() - (UTC_DAY < UTC_SOD ? (7 - UTC_SOD) + UTC_DAY : UTC_DAY - UTC_SOD), 0, 0, 0, 0));
40
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate() - (UTC_DAY < UTC_SOD ? (7 - UTC_SOD) + UTC_DAY : UTC_DAY - UTC_SOD)));
39
41
  }
40
42
  case 'day':
41
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), 0, 0, 0, 0));
43
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate()));
42
44
  case 'hour':
43
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), 0, 0, 0));
45
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate(), n_val.getUTCHours()));
44
46
  case 'minute':
45
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), 0, 0));
47
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate(), n_val.getUTCHours(), n_val.getUTCMinutes()));
46
48
  case 'second':
47
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), 0));
49
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate(), n_val.getUTCHours(), n_val.getUTCMinutes(), n_val.getUTCSeconds()));
48
50
  default:
49
- return new Date(Date.UTC(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds()));
51
+ return new Date(Date.UTC(year, n_val.getUTCMonth(), n_val.getUTCDate(), n_val.getUTCHours(), n_val.getUTCMinutes(), n_val.getUTCSeconds(), n_val.getUTCMilliseconds()));
50
52
  }
51
53
  }
@@ -2,7 +2,11 @@ type toObjectConfig = {
2
2
  /**
3
3
  * Pass array of keys that should not be normalized into number/bool when seen
4
4
  */
5
- raw?: string[];
5
+ raw?: string[] | true;
6
+ /**
7
+ * Pass array of keys that should only have a single value (e.g., 'action')
8
+ */
9
+ single?: string[];
6
10
  };
7
11
  /**
8
12
  * Converts a FormData instance to a json object
@@ -3,13 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.toObject = toObject;
4
4
  exports.default = toObject;
5
5
  const RGX_CLOSE = /\]/g;
6
- function assignValue(acc, rawkey, value) {
6
+ function assignValue(acc, rawkey, value, single) {
7
7
  let cursor = acc;
8
8
  const keys = rawkey.replace(RGX_CLOSE, '').split(/\[|\./);
9
9
  for (let i = 0; i < keys.length; i++) {
10
10
  const key = keys[i];
11
11
  if (i === keys.length - 1) {
12
- if (cursor[key] !== undefined) {
12
+ if (cursor[key] !== undefined && (!single || !single.has(key))) {
13
13
  if (Array.isArray(cursor[key])) {
14
14
  cursor[key].push(value);
15
15
  }
@@ -38,11 +38,12 @@ function assignValue(acc, rawkey, value) {
38
38
  function toObject(form, config) {
39
39
  if (!(form instanceof FormData))
40
40
  throw new Error('formdata/toObject: Value is not an instance of FormData');
41
- const set = new Set(Array.isArray(config?.raw) ? config.raw : []);
41
+ const set = config?.raw === true ? true : new Set(Array.isArray(config?.raw) ? config?.raw : []);
42
+ const single = Array.isArray(config?.single) && config?.single.length ? new Set(config.single) : null;
42
43
  const acc = {};
43
44
  form.forEach((value, key) => {
44
45
  let normalizedValue = value;
45
- if (typeof value === 'string' && !set.has(key)) {
46
+ if (set !== true && typeof value === 'string' && !set.has(key)) {
46
47
  const lower = value.toLowerCase();
47
48
  normalizedValue = (lower === 'true'
48
49
  ? true
@@ -52,7 +53,7 @@ function toObject(form, config) {
52
53
  ? Number(value)
53
54
  : value);
54
55
  }
55
- assignValue(acc, key, normalizedValue);
56
+ assignValue(acc, key, normalizedValue, single);
56
57
  });
57
58
  return acc;
58
59
  }
package/hash/fnv1A.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ export declare const FNV_32 = 2166136261;
2
+ export declare const FNV_64 = 1099511628211;
1
3
  /**
2
4
  * Convert a provided value into a Fowler-Noll-Vo 1A hash
3
5
  * For more info: https://tools.ietf.org/html/draft-eastlake-fnv-03
package/hash/fnv1A.js CHANGED
@@ -1,14 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FNV_64 = exports.FNV_32 = void 0;
3
4
  exports.fnv1A = fnv1A;
4
5
  exports.default = fnv1A;
5
- const FNV_32 = 2166136261;
6
+ exports.FNV_32 = 2166136261;
7
+ exports.FNV_64 = 1099511628211;
6
8
  const REPL_NAN = 'nan';
7
9
  const REPL_TRUE = 'true';
8
10
  const REPL_FALSE = 'false';
9
11
  const REPL_UNDEF = 'undefined';
10
12
  const REPL_NULL = 'null';
11
- function fnv1A(data, offset = FNV_32) {
13
+ function fnv1A(data, offset = exports.FNV_32) {
12
14
  let hash = offset;
13
15
  let sanitized;
14
16
  switch (typeof data) {
package/index.d.ts CHANGED
@@ -66,7 +66,7 @@ declare module "array/mapPrimitive" {
66
66
  export { mapPrimitive, mapPrimitive as default };
67
67
  }
68
68
  declare module "object/isNotEmpty" {
69
- function isNotEmptyObject(val: unknown): val is Record<string, any>;
69
+ function isNotEmptyObject<T extends Record<string, any>>(val: T | unknown): val is T;
70
70
  export { isNotEmptyObject, isNotEmptyObject as default };
71
71
  }
72
72
  declare module "array/groupBy" {
@@ -86,9 +86,7 @@ declare module "array/split" {
86
86
  export { split, split as default };
87
87
  }
88
88
  declare module "object/is" {
89
- function isObject(val: unknown): val is {
90
- [key: string]: any;
91
- };
89
+ function isObject<T extends Record<string, any>>(val: T | unknown): val is T;
92
90
  export { isObject, isObject as default };
93
91
  }
94
92
  declare module "array/sort" {
@@ -129,19 +127,23 @@ declare module "date/is" {
129
127
  function isDate(val: unknown): val is Date;
130
128
  export { isDate, isDate as default };
131
129
  }
130
+ declare module "date/convertToDate" {
131
+ function convertToDate(val: Date | string | number): Date | null;
132
+ export { convertToDate, convertToDate as default };
133
+ }
132
134
  declare module "date/addUTC" {
133
135
  export type AddUTCKey = 'years' | 'year' | 'months' | 'month' | 'days' | 'day' | 'hours' | 'hour' | 'minutes' | 'minute' | 'seconds' | 'second' | 'milliseconds' | 'millisecond';
134
- function addUTC(val: Date, amt?: number, key?: AddUTCKey): Date;
136
+ function addUTC(val: Date | string, amt?: number, key?: AddUTCKey): Date;
135
137
  export { addUTC, addUTC as default };
136
138
  }
137
139
  declare module "date/diff" {
138
140
  export type DiffKey = 'week' | 'weeks' | 'day' | 'days' | 'hour' | 'hours' | 'minute' | 'minutes' | 'second' | 'seconds' | 'millisecond' | 'milliseconds';
139
- function diff(val_a: Date, val_b: Date, key?: DiffKey): number;
141
+ function diff(val_a: Date | string, val_b: Date | string, key?: DiffKey): number;
140
142
  export { diff, diff as default };
141
143
  }
142
144
  declare module "date/endOfUTC" {
143
145
  export type EndOfUTCKey = 'year' | 'quarter' | 'month' | 'week' | 'week_sun' | 'week_mon' | 'week_tue' | 'week_wed' | 'week_thu' | 'week_fri' | 'week_sat' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond';
144
- function endOfUTC(val: Date, key?: EndOfUTCKey): Date;
146
+ function endOfUTC(val: Date | string, key?: EndOfUTCKey): Date;
145
147
  export { endOfUTC, endOfUTC as default };
146
148
  }
147
149
  declare module "date/format" {
@@ -151,7 +153,7 @@ declare module "date/format" {
151
153
  readonly sat: "sat";
152
154
  };
153
155
  export type WEEK_START = keyof typeof WEEK_STARTS;
154
- function format(val: Date, spec: string, locale?: string, zone?: string, sow?: WEEK_START): string;
156
+ function format(val: Date | string, spec: string, locale?: string, zone?: string, sow?: WEEK_START): string;
155
157
  namespace format {
156
158
  var getLocale: () => string;
157
159
  var setLocale: (locale: string) => void;
@@ -167,7 +169,7 @@ declare module "date/isFormat" {
167
169
  export { isDateFormat, isDateFormat as default };
168
170
  }
169
171
  declare module "date/isLeap" {
170
- function isLeap(val: Date): boolean;
172
+ function isLeap(val: Date | string): boolean;
171
173
  export { isLeap, isLeap as default };
172
174
  }
173
175
  declare module "date/nowUnix" {
@@ -189,12 +191,12 @@ declare module "date/setTimeUTC" {
189
191
  second?: number;
190
192
  millisecond?: number;
191
193
  };
192
- function setTimeUTC(val: Date, props: TimeProps): Date;
194
+ function setTimeUTC(val: Date | string, props: TimeProps): Date;
193
195
  export { setTimeUTC, setTimeUTC as default };
194
196
  }
195
197
  declare module "date/startOfUTC" {
196
198
  export type StartOfUTCKey = 'year' | 'quarter' | 'month' | 'week' | 'week_sun' | 'week_mon' | 'week_tue' | 'week_wed' | 'week_thu' | 'week_fri' | 'week_sat' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond';
197
- function startOfUTC(val: Date, key?: StartOfUTCKey): Date;
199
+ function startOfUTC(val: Date | string, key?: StartOfUTCKey): Date;
198
200
  export { startOfUTC, startOfUTC as default };
199
201
  }
200
202
  declare module "date/toUnix" {
@@ -207,6 +209,7 @@ declare module "date/toUTC" {
207
209
  }
208
210
  declare module "date/index" {
209
211
  import { addUTC } from "date/addUTC";
212
+ import { convertToDate } from "date/convertToDate";
210
213
  import { diff } from "date/diff";
211
214
  import { endOfUTC } from "date/endOfUTC";
212
215
  import { format } from "date/format";
@@ -219,7 +222,7 @@ declare module "date/index" {
219
222
  import { startOfUTC } from "date/startOfUTC";
220
223
  import { toUnix } from "date/toUnix";
221
224
  import { toUTC } from "date/toUTC";
222
- export { addUTC, diff, endOfUTC, format, isDateFormat as isFormat, isDateFormat, isDate, isDate as is, isLeap, nowUnix, nowUnixMs, setTimeUTC, startOfUTC, toUnix, toUTC };
225
+ export { addUTC, convertToDate, diff, endOfUTC, format, isDateFormat as isFormat, isDateFormat, isDate, isDate as is, isLeap, nowUnix, nowUnixMs, setTimeUTC, startOfUTC, toUnix, toUTC };
223
226
  }
224
227
  declare module "formdata/is" {
225
228
  function isFormData(val: unknown): val is FormData;
@@ -227,7 +230,8 @@ declare module "formdata/is" {
227
230
  }
228
231
  declare module "formdata/toObject" {
229
232
  type toObjectConfig = {
230
- raw?: string[];
233
+ raw?: string[] | true;
234
+ single?: string[];
231
235
  };
232
236
  function toObject<T extends Record<string, unknown>>(form: FormData, config?: toObjectConfig): T;
233
237
  export { toObject, toObject as default };
@@ -328,9 +332,13 @@ declare module "object/pick" {
328
332
  type DottedKeys<T> = (T extends ObjectType ? {
329
333
  [K in keyof T & string]: T[K] extends ObjectType ? K | `${K}.${DottedKeys<T[K]>}` : K;
330
334
  }[keyof T & string] : string) & string;
331
- function pick<T extends Record<string, any>, K extends DottedKeys<T>>(obj: T, keys: K[]): {
332
- [key: string]: any;
333
- };
335
+ type PickFromObject<T, K extends string> = K extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? T[Key] extends ObjectType ? {
336
+ [P in Key]: PickFromObject<T[Key], Rest>;
337
+ } : object : object : K extends keyof T ? {
338
+ [P in K]: T[K];
339
+ } : object;
340
+ type UnionToIntersection<U> = (U extends any ? (k: U) => void : object) extends (k: infer I) => void ? I : object;
341
+ function pick<T extends Record<string, any>, K extends readonly DottedKeys<T>[]>(obj: T, keys: K): UnionToIntersection<PickFromObject<T, K[number]>>;
334
342
  export { pick, pick as default };
335
343
  }
336
344
  declare module "object/index" {
@@ -569,6 +577,8 @@ declare module "deep/index" {
569
577
  export { deepFreeze as freeze, deepFreeze, deepGet as get, deepGet, deepSeal as seal, deepSeal, deepSet as set, deepSet };
570
578
  }
571
579
  declare module "hash/fnv1A" {
580
+ export const FNV_32 = 2166136261;
581
+ export const FNV_64 = 1099511628211;
572
582
  function fnv1A(data: unknown, offset?: number): number;
573
583
  export { fnv1A, fnv1A as default };
574
584
  }
package/object/is.d.ts CHANGED
@@ -3,7 +3,5 @@
3
3
  *
4
4
  * @param {unknown} val - Value to verify
5
5
  */
6
- declare function isObject(val: unknown): val is {
7
- [key: string]: any;
8
- };
6
+ declare function isObject<T extends Record<string, any>>(val: T | unknown): val is T;
9
7
  export { isObject, isObject as default };
@@ -3,5 +3,5 @@
3
3
  *
4
4
  * @param {unknown} val - Value to verify
5
5
  */
6
- declare function isNotEmptyObject(val: unknown): val is Record<string, any>;
6
+ declare function isNotEmptyObject<T extends Record<string, any>>(val: T | unknown): val is T;
7
7
  export { isNotEmptyObject, isNotEmptyObject as default };
package/object/pick.d.ts CHANGED
@@ -4,13 +4,17 @@ type ObjectType = {
4
4
  type DottedKeys<T> = (T extends ObjectType ? {
5
5
  [K in keyof T & string]: T[K] extends ObjectType ? K | `${K}.${DottedKeys<T[K]>}` : K;
6
6
  }[keyof T & string] : string) & string;
7
+ type PickFromObject<T, K extends string> = K extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? T[Key] extends ObjectType ? {
8
+ [P in Key]: PickFromObject<T[Key], Rest>;
9
+ } : object : object : K extends keyof T ? {
10
+ [P in K]: T[K];
11
+ } : object;
12
+ type UnionToIntersection<U> = (U extends any ? (k: U) => void : object) extends (k: infer I) => void ? I : object;
7
13
  /**
8
14
  * Returns a new object with the keys picked from the passed object
9
15
  *
10
16
  * @param obj - Object to pick from
11
17
  * @param keys - Array of keys to pick from object
12
18
  */
13
- declare function pick<T extends Record<string, any>, K extends DottedKeys<T>>(obj: T, keys: K[]): {
14
- [key: string]: any;
15
- };
19
+ declare function pick<T extends Record<string, any>, K extends readonly DottedKeys<T>[]>(obj: T, keys: K): UnionToIntersection<PickFromObject<T, K[number]>>;
16
20
  export { pick, pick as default };
package/object/pick.js CHANGED
@@ -16,24 +16,24 @@ function pick(obj, keys) {
16
16
  if (typeof key !== 'string')
17
17
  continue;
18
18
  sanitized = key.trim();
19
- if (!sanitized.length)
19
+ if (!sanitized)
20
20
  continue;
21
21
  if (sanitized.indexOf('.') >= 0) {
22
22
  val = (0, get_1.deepGet)(obj, sanitized);
23
23
  if (val === undefined)
24
24
  continue;
25
- const parts = key.split('.');
25
+ const parts = sanitized.split('.');
26
26
  const parts_len = parts.length;
27
27
  let cursor = map;
28
28
  for (let y = 0; y < parts_len - 1; y++) {
29
29
  const part = parts[y].trim();
30
- if (!cursor[part])
30
+ if (!(part in cursor))
31
31
  cursor[part] = {};
32
32
  cursor = cursor[part];
33
33
  }
34
34
  cursor[parts[parts_len - 1].trim()] = val;
35
35
  }
36
- else if (obj[sanitized] !== undefined) {
36
+ else if (sanitized in obj) {
37
37
  map[sanitized] = obj[sanitized];
38
38
  }
39
39
  }
package/package.json CHANGED
@@ -1 +1 @@
1
- { "name": "@valkyriestudios/utils", "version": "12.22.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "url": "https://www.linkedin.com/in/petervermeulen1/" }, "keywords": [ "utility", "library", "javascript", "js", "node", "bun" ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/ValkyrieStudios/utils.git" }, "bugs": { "url": "https://github.com/ValkyrieStudios/utils/issues" }, "homepage": "https://github.com/ValkyrieStudios/utils#readme", "types": "index.d.ts" }
1
+ { "name": "@valkyriestudios/utils", "version": "12.24.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "url": "https://www.linkedin.com/in/petervermeulen1/" }, "keywords": [ "utility", "library", "javascript", "js", "node", "bun" ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/ValkyrieStudios/utils.git" }, "bugs": { "url": "https://github.com/ValkyrieStudios/utils/issues" }, "homepage": "https://github.com/ValkyrieStudios/utils#readme", "types": "index.d.ts" }