@valkyriestudios/utils 12.11.0 → 12.12.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
@@ -431,6 +431,19 @@ Returns the current unix timestamp in seconds
431
431
  - **nowUnixMs()**
432
432
  Returns the current unix timestamp in milliseconds
433
433
 
434
+ - **setTimeUTC(val:Date, props:{hour?:number;minute?:number;second?:number;millisecond?:number})**
435
+ Take the incoming date and return a date where the time portion is set to the values in the provided props
436
+
437
+ Note: Does not touch the date object passed
438
+ ```typescript
439
+ setTimeUTC(new Date("2023-05-04T12:04:27.432Z"), {hour: 5}); // new Date("2023-05-04T05:04:27.432Z")
440
+ setTimeUTC(new Date("2023-05-04T12:04:27.432Z"), {hour: 5, minute: 30}); // new Date("2023-05-04T05:30:27.432Z")
441
+ setTimeUTC(new Date("2023-05-04T12:04:27.432Z"), {hour: 5, minute: 30, second: 0}); // new Date("2023-05-04T05:30:00.432Z")
442
+ setTimeUTC(new Date("2023-05-04T12:04:27.432Z"), {hour: 5, minute: 30, second: 0, millisecond: 0}); // new Date("2023-05-04T05:30:00.000Z")
443
+ setTimeUTC(new Date("2023-05-04T12:04:27.432Z"), {minute: 30, second: 0, millisecond: 0}); // new Date("2023-05-04T12:30:00.000Z")
444
+ setTimeUTC(new Date("2023-05-04T12:04:27.432Z"), {second: 9, millisecond: 0}); // new Date("2023-05-04T12:04:09.000Z")
445
+ ```
446
+
434
447
  - **startOfUTC(val:Date, key:string)**
435
448
  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).
436
449
 
package/date/format.js CHANGED
@@ -41,16 +41,16 @@ function toZone(date, zone) {
41
41
  function runIntl(loc, token, props, val) {
42
42
  const hash = `${loc}:${token}`;
43
43
  let formatter = intl_formatters.get(hash);
44
- if (formatter)
45
- return formatter.format(val);
46
- try {
47
- formatter = new Intl.DateTimeFormat(loc, props);
48
- intl_formatters.set(hash, formatter);
49
- return formatter.format(val);
50
- }
51
- catch (err) {
52
- throw new Error(`format: Failed to run conversion for ${token} with locale ${loc}`);
44
+ if (!formatter) {
45
+ try {
46
+ formatter = new Intl.DateTimeFormat(loc, props);
47
+ intl_formatters.set(hash, formatter);
48
+ }
49
+ catch (err) {
50
+ throw new Error(`format: Failed to run conversion for ${token} with locale ${loc}`);
51
+ }
53
52
  }
53
+ return formatter.format(val);
54
54
  }
55
55
  const Tokens = [
56
56
  ['YYYY', d => d.getFullYear()],
@@ -83,11 +83,13 @@ function getSpecChain(spec) {
83
83
  return spec_chain;
84
84
  spec_chain = [];
85
85
  let cursor;
86
+ let spec_cursor = spec;
86
87
  for (let i = 0; i < Tokens.length; i++) {
87
88
  cursor = Tokens[i];
88
- if (spec.indexOf(cursor[0]) < 0)
89
+ if (spec_cursor.indexOf(cursor[0]) < 0)
89
90
  continue;
90
91
  spec_chain.push(cursor);
92
+ spec_cursor = spec_cursor.replace(cursor[1], '');
91
93
  }
92
94
  if (spec_chain.length === 0)
93
95
  return false;
package/date/index.d.ts CHANGED
@@ -5,7 +5,8 @@ import { format } from './format';
5
5
  import { isDate } from './is';
6
6
  import { nowUnix } from './nowUnix';
7
7
  import { nowUnixMs } from './nowUnixMs';
8
+ import { setTimeUTC } from './setTimeUTC';
8
9
  import { startOfUTC } from './startOfUTC';
9
10
  import { toUnix } from './toUnix';
10
11
  import { toUTC } from './toUTC';
11
- export { addUTC, diff, endOfUTC, format, isDate, isDate as is, nowUnix, nowUnixMs, startOfUTC, toUnix, toUTC };
12
+ export { addUTC, diff, endOfUTC, format, isDate, isDate as is, nowUnix, nowUnixMs, setTimeUTC, startOfUTC, toUnix, toUTC };
package/date/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toUTC = exports.toUnix = exports.startOfUTC = exports.nowUnixMs = exports.nowUnix = exports.is = exports.isDate = exports.format = exports.endOfUTC = exports.diff = exports.addUTC = void 0;
3
+ exports.toUTC = exports.toUnix = exports.startOfUTC = exports.setTimeUTC = exports.nowUnixMs = exports.nowUnix = exports.is = exports.isDate = exports.format = exports.endOfUTC = exports.diff = 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
6
  const diff_1 = require("./diff");
@@ -16,6 +16,8 @@ const nowUnix_1 = require("./nowUnix");
16
16
  Object.defineProperty(exports, "nowUnix", { enumerable: true, get: function () { return nowUnix_1.nowUnix; } });
17
17
  const nowUnixMs_1 = require("./nowUnixMs");
18
18
  Object.defineProperty(exports, "nowUnixMs", { enumerable: true, get: function () { return nowUnixMs_1.nowUnixMs; } });
19
+ const setTimeUTC_1 = require("./setTimeUTC");
20
+ Object.defineProperty(exports, "setTimeUTC", { enumerable: true, get: function () { return setTimeUTC_1.setTimeUTC; } });
19
21
  const startOfUTC_1 = require("./startOfUTC");
20
22
  Object.defineProperty(exports, "startOfUTC", { enumerable: true, get: function () { return startOfUTC_1.startOfUTC; } });
21
23
  const toUnix_1 = require("./toUnix");
@@ -0,0 +1,15 @@
1
+ export type TimeProps = {
2
+ hour?: number;
3
+ minute?: number;
4
+ second?: number;
5
+ millisecond?: number;
6
+ };
7
+ /**
8
+ * Sets the time on a provided date object and returns it
9
+ *
10
+ * @param {Date} val - Date to set the time for
11
+ * @param {Time} props - Time props to set the time to
12
+ * @returns {Date} New date with provided amount of key added
13
+ */
14
+ declare function setTimeUTC(val: Date, props: TimeProps): Date;
15
+ export { setTimeUTC, setTimeUTC as default };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setTimeUTC = setTimeUTC;
4
+ exports.default = setTimeUTC;
5
+ const is_1 = require("./is");
6
+ const isIntegerBetween_1 = require("../number/isIntegerBetween");
7
+ function setTimeUTC(val, props) {
8
+ if (!(0, is_1.isDate)(val))
9
+ 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
+ }
package/index.d.ts CHANGED
@@ -146,6 +146,20 @@ declare module "date/nowUnixMs" {
146
146
  function nowUnixMs(): number;
147
147
  export { nowUnixMs, nowUnixMs as default };
148
148
  }
149
+ declare module "number/isIntegerBetween" {
150
+ function isIntegerBetween(val: unknown, min: number, max: number): val is number;
151
+ export { isIntegerBetween, isIntegerBetween as default };
152
+ }
153
+ declare module "date/setTimeUTC" {
154
+ export type TimeProps = {
155
+ hour?: number;
156
+ minute?: number;
157
+ second?: number;
158
+ millisecond?: number;
159
+ };
160
+ function setTimeUTC(val: Date, props: TimeProps): Date;
161
+ export { setTimeUTC, setTimeUTC as default };
162
+ }
149
163
  declare module "date/startOfUTC" {
150
164
  function startOfUTC(val: Date, key?: 'year' | 'quarter' | 'month' | 'week' | 'week_sun' | 'week_mon' | 'week_tue' | 'week_wed' | 'week_thu' | 'week_fri' | 'week_sat' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond'): Date;
151
165
  export { startOfUTC, startOfUTC as default };
@@ -166,10 +180,11 @@ declare module "date/index" {
166
180
  import { isDate } from "date/is";
167
181
  import { nowUnix } from "date/nowUnix";
168
182
  import { nowUnixMs } from "date/nowUnixMs";
183
+ import { setTimeUTC } from "date/setTimeUTC";
169
184
  import { startOfUTC } from "date/startOfUTC";
170
185
  import { toUnix } from "date/toUnix";
171
186
  import { toUTC } from "date/toUTC";
172
- export { addUTC, diff, endOfUTC, format, isDate, isDate as is, nowUnix, nowUnixMs, startOfUTC, toUnix, toUTC };
187
+ export { addUTC, diff, endOfUTC, format, isDate, isDate as is, nowUnix, nowUnixMs, setTimeUTC, startOfUTC, toUnix, toUTC };
173
188
  }
174
189
  declare module "formdata/is" {
175
190
  function isFormData(val: unknown): val is FormData;
@@ -319,10 +334,6 @@ declare module "number/isIntegerBelowOrEqual" {
319
334
  function isIntegerBelowOrEqual(val: unknown, ref: number): val is number;
320
335
  export { isIntegerBelowOrEqual, isIntegerBelowOrEqual as default };
321
336
  }
322
- declare module "number/isIntegerBetween" {
323
- function isIntegerBetween(val: unknown, min: number, max: number): val is number;
324
- export { isIntegerBetween, isIntegerBetween as default };
325
- }
326
337
  declare module "number/randomBetween" {
327
338
  function randomBetween(min?: number, max?: number): number;
328
339
  export { randomBetween, randomBetween as default };
package/package.json CHANGED
@@ -1 +1 @@
1
- { "name": "@valkyriestudios/utils", "version": "12.11.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.12.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" }