@valkyriestudios/utils 7.1.0 → 7.2.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/CHANGELOG.md CHANGED
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic
6
6
  Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [7.2.0] - 2023-06-11
9
+ ### Added
10
+ - date/toUnix: Convert a date to its corresponding unix timestamp in seconds (1.000.000 ops in ~300ms)
11
+ - date/diff (supports week,day,hour,minute,millisecond) (1.000.000 ops in ~650ms)
12
+
13
+ ### Improved
14
+ - Dep: Upgrade @babel/cli to 7.22.5
15
+ - Dep: Upgrade @babel/core to 7.22.5
16
+ - Dep: Upgrade @babel/preset-env to 7.22.5
17
+ - Dep: Upgrade @babel/register to 7.22.5
18
+ - Dep: Upgrade eslint to 8.42.0
19
+
8
20
  ## [7.1.0] - 2023-05-13
9
21
  ### Added
10
22
  - date/addUTC (1.000.000 ops in ~750ms)
package/README.md CHANGED
@@ -278,9 +278,26 @@ isDate(new Date('December 17, 1995 03:24:00'); // TRUE
278
278
  isDate('December 17, 1995 03:24:00'); // FALSE
279
279
  ```
280
280
 
281
+ - **diff(val_a:Date, val_b:Date, key:String)**
282
+ Take two incoming dates and return the difference between them in a certain unit. Possible key options(week,day,hour,minute,second,millisecond).
283
+
284
+ Note: Does not touch the passed date objects, if no key is passed will default to millisecond
285
+ ```js
286
+ diff(new Date("2022-10-05T13:12:11+02:00"), new Date("2022-11-05T13:12:11+06:00"), 'week'); // -4.404761904761905
287
+ diff(new Date("2022-11-05T13:12:11+06:00"), new Date("2022-10-05T13:12:11+02:00"), 'day'); // 30.83333333333333332
288
+ diff(new Date("2022-11-05T13:12:11+06:00"), new Date("2022-10-05T13:12:11+02:00"), 'hour'); // 740
289
+ diff(new Date("2022-10-05T13:12:11+02:00"), new Date("2022-10-05T17:43:09.344+06:00"), 'minute'); // -30.9724
290
+ diff(new Date("2022-10-05T13:12:11+02:00"), new Date("2022-10-05T17:43:09.344+06:00"), 'second'); // -1858.344
291
+ diff(new Date("2022-10-05T13:12:11+02:00"), new Date("2022-10-05T17:43:09.344+06:00"), 'millisecond'); // -1858344
292
+ diff(new Date("2022-11-05T13:12:11+06:00"), new Date("2022-10-05T13:25:43.898+02:00")); // 2663187102
293
+ ```
294
+
281
295
  - **toUTC(val:Date)**
282
296
  Takes the passed date object and returns a new date object set for utc
283
297
 
298
+ - **toUnix(val:Date)**
299
+ Takes the passed date object and returns its unix timestamp in seconds
300
+
284
301
  - **nowUnix()**
285
302
  Returns the current unix timestamp in seconds
286
303
 
package/date/diff.js ADDED
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: !0
5
+ });
6
+ exports["default"] = diff;
7
+ var _is = _interopRequireDefault(require("./is"));
8
+ var _isNotEmpty = _interopRequireDefault(require("../string/isNotEmpty"));
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
10
+ var SECOND_IN_MILLISECONDS = 1000;
11
+ var MINUTE_IN_MILLISECONDS = SECOND_IN_MILLISECONDS * 60;
12
+ var HOUR_IN_MILLISECONDS = MINUTE_IN_MILLISECONDS * 60;
13
+ var DAY_IN_MILLISECONDS = HOUR_IN_MILLISECONDS * 24;
14
+ var WEEK_IN_MILLISECONDS = DAY_IN_MILLISECONDS * 7;
15
+ function diff(val_a, val_b) {
16
+ var key = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : !1;
17
+ if (!(0, _is["default"])(val_a) || !(0, _is["default"])(val_b)) throw new Error('Diff requires date objects for both values');
18
+ if (key !== !1 && !(0, _isNotEmpty["default"])(key)) throw new Error('Key needs to be a string or false');
19
+ var diff_in_ms = val_a.valueOf() - val_b.valueOf();
20
+ switch (key) {
21
+ case 'week':
22
+ case 'weeks':
23
+ return diff_in_ms / WEEK_IN_MILLISECONDS;
24
+ case 'day':
25
+ case 'days':
26
+ return diff_in_ms / DAY_IN_MILLISECONDS;
27
+ case 'hour':
28
+ case 'hours':
29
+ return diff_in_ms / HOUR_IN_MILLISECONDS;
30
+ case 'minute':
31
+ case 'minutes':
32
+ return diff_in_ms / MINUTE_IN_MILLISECONDS;
33
+ case 'second':
34
+ case 'seconds':
35
+ return diff_in_ms / SECOND_IN_MILLISECONDS;
36
+ case 'millisecond':
37
+ case 'milliseconds':
38
+ default:
39
+ return diff_in_ms;
40
+ }
41
+ }
package/date/toUnix.js ADDED
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: !0
5
+ });
6
+ exports["default"] = toUnix;
7
+ var _is = _interopRequireDefault(require("./is"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9
+ function toUnix(val) {
10
+ if (!(0, _is["default"])(val)) throw new Error('toUnix requires a date object');
11
+ return Math.floor(val.valueOf() / 1000);
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valkyriestudios/utils",
3
- "version": "7.1.0",
3
+ "version": "7.2.0",
4
4
  "description": "A collection of single-function utilities for common tasks",
5
5
  "main": "index.js",
6
6
  "author": {
@@ -32,10 +32,10 @@
32
32
  },
33
33
  "homepage": "https://github.com/ValkyrieStudios/utils#readme",
34
34
  "devDependencies": {
35
- "@babel/cli": "^7.21.5",
36
- "@babel/core": "^7.21.8",
37
- "@babel/preset-env": "^7.21.5",
38
- "@babel/register": "^7.21.0",
35
+ "@babel/cli": "^7.22.5",
36
+ "@babel/core": "^7.22.5",
37
+ "@babel/preset-env": "^7.22.5",
38
+ "@babel/register": "^7.22.5",
39
39
  "babel-plugin-check-es2015-constants": "^6.22.0",
40
40
  "babel-plugin-transform-member-expression-literals": "^6.9.4",
41
41
  "babel-plugin-transform-minify-booleans": "^6.9.4",
@@ -44,7 +44,7 @@
44
44
  "chai": "^4.3.7",
45
45
  "chai-as-promised": "^7.1.1",
46
46
  "chai-spies": "^1.0.0",
47
- "eslint": "^8.40.0",
47
+ "eslint": "^8.42.0",
48
48
  "mocha": "^10.2.0",
49
49
  "nyc": "^15.1.0"
50
50
  }