@wener/utils 1.1.56 → 1.1.58

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.
@@ -1,14 +1,14 @@
1
1
  export function getFileFromDataTransfer(dataTransfer) {
2
+ var _dataTransfer_items;
2
3
  if (!dataTransfer) {
3
4
  return {};
4
5
  }
5
- var _dataTransfer_items;
6
6
  var items = (_dataTransfer_items = dataTransfer.items) !== null && _dataTransfer_items !== void 0 ? _dataTransfer_items : [];
7
7
  if (items.length >= 2 && items[0].kind === 'string' && items[1].kind === 'file') {
8
+ var _items__getAsFile;
8
9
  var _dataTransfer_files;
9
10
  // name, file
10
11
  var text = dataTransfer.getData('text');
11
- var _items__getAsFile;
12
12
  var file = (_items__getAsFile = items[1].getAsFile()) !== null && _items__getAsFile !== void 0 ? _items__getAsFile : (_dataTransfer_files = dataTransfer.files) === null || _dataTransfer_files === void 0 ? void 0 : _dataTransfer_files.item(0);
13
13
  if (!file) {
14
14
  console.error("no file ".concat(text), items[1]);
@@ -1 +1,64 @@
1
1
  // https://github.com/LiosK/uuidv7/blob/main/src/index.ts
2
+ // https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-version-7
3
+ import { getRandomValues } from "../web/getRandomValues.js";
4
+ /**
5
+ * Generate a UUIDv7 string
6
+ *
7
+ * UUIDv7 format (RFC 9562):
8
+ * - 48 bits: Unix timestamp in milliseconds
9
+ * - 4 bits: version (7)
10
+ * - 12 bits: random (rand_a)
11
+ * - 2 bits: variant (10)
12
+ * - 62 bits: random (rand_b)
13
+ *
14
+ * Format: xxxxxxxx-xxxx-7xxx-yxxx-xxxxxxxxxxxx
15
+ * where y is 8, 9, a, or b (variant bits)
16
+ */ export function randomUUIDv7(timestamp) {
17
+ var ts = timestamp !== null && timestamp !== void 0 ? timestamp : Date.now();
18
+ var bytes = new Uint8Array(16);
19
+ getRandomValues(bytes);
20
+ // timestamp (48 bits)
21
+ bytes[0] = ts / Math.pow(2, 40) & 255;
22
+ bytes[1] = ts / Math.pow(2, 32) & 255;
23
+ bytes[2] = ts / Math.pow(2, 24) & 255;
24
+ bytes[3] = ts / Math.pow(2, 16) & 255;
25
+ bytes[4] = ts / Math.pow(2, 8) & 255;
26
+ bytes[5] = ts & 255;
27
+ // version (4 bits) = 7
28
+ bytes[6] = bytes[6] & 15 | 112;
29
+ // variant (2 bits) = 10
30
+ bytes[8] = bytes[8] & 63 | 128;
31
+ return formatUUID(bytes);
32
+ }
33
+ function formatUUID(bytes) {
34
+ var hex = Array.from(bytes).map(function (b) {
35
+ return b.toString(16).padStart(2, "0");
36
+ }).join("");
37
+ return "".concat(hex.slice(0, 8), "-").concat(hex.slice(8, 12), "-").concat(hex.slice(12, 16), "-").concat(hex.slice(16, 20), "-").concat(hex.slice(20));
38
+ }
39
+ /**
40
+ * Extract timestamp from UUIDv7
41
+ */ export function parseUUIDv7Timestamp(uuid) {
42
+ var hex = uuid.replace(/-/g, "");
43
+ if (hex.length !== 32) {
44
+ throw new Error("Invalid UUID format");
45
+ }
46
+ var tsHex = hex.slice(0, 12);
47
+ return parseInt(tsHex, 16);
48
+ }
49
+ /**
50
+ * Check if a string is a valid UUIDv7
51
+ */ export function isUUIDv7(uuid) {
52
+ if (!uuid)
53
+ return false;
54
+ var match = uuid.match(/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
55
+ return match !== null;
56
+ }
57
+ /**
58
+ * Create a UUIDv7 generator with custom options
59
+ */ export function createRandomUUIDv7() {
60
+ var _ref = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, _ref_now = _ref.now, now = _ref_now === void 0 ? Date.now : _ref_now;
61
+ return function uuidv7(timestamp) {
62
+ return randomUUIDv7(timestamp !== null && timestamp !== void 0 ? timestamp : now());
63
+ };
64
+ }
@@ -0,0 +1,79 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { createRandomUUIDv7, isUUIDv7, parseUUIDv7Timestamp, randomUUIDv7 } from "./randomUUIDv7.js";
3
+ describe("randomUUIDv7", function () {
4
+ test("generates valid UUIDv7", function () {
5
+ var uuid = randomUUIDv7();
6
+ expect(uuid).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
7
+ });
8
+ test("generates unique UUIDs", function () {
9
+ var uuids = new Set();
10
+ for (var i = 0; i < 1000; i++) {
11
+ uuids.add(randomUUIDv7());
12
+ }
13
+ expect(uuids.size).toBe(1000);
14
+ });
15
+ test("respects provided timestamp", function () {
16
+ var ts = 1704067200000; // 2024-01-01 00:00:00 UTC
17
+ var uuid = randomUUIDv7(ts);
18
+ var extractedTs = parseUUIDv7Timestamp(uuid);
19
+ expect(extractedTs).toBe(ts);
20
+ });
21
+ test("UUIDs are sortable by time", function () {
22
+ var ts1 = 1704067200000;
23
+ var ts2 = 1704067201000;
24
+ var uuid1 = randomUUIDv7(ts1);
25
+ var uuid2 = randomUUIDv7(ts2);
26
+ expect(uuid1 < uuid2).toBe(true);
27
+ });
28
+ });
29
+ describe("isUUIDv7", function () {
30
+ test("returns true for valid UUIDv7", function () {
31
+ var uuid = randomUUIDv7();
32
+ expect(isUUIDv7(uuid)).toBe(true);
33
+ });
34
+ test("returns false for UUIDv4", function () {
35
+ expect(isUUIDv7("550e8400-e29b-41d4-a716-446655440000")).toBe(false);
36
+ });
37
+ test("returns false for invalid strings", function () {
38
+ expect(isUUIDv7("")).toBe(false);
39
+ expect(isUUIDv7(null)).toBe(false);
40
+ expect(isUUIDv7(undefined)).toBe(false);
41
+ expect(isUUIDv7("not-a-uuid")).toBe(false);
42
+ });
43
+ });
44
+ describe("parseUUIDv7Timestamp", function () {
45
+ test("extracts correct timestamp", function () {
46
+ var ts = Date.now();
47
+ var uuid = randomUUIDv7(ts);
48
+ expect(parseUUIDv7Timestamp(uuid)).toBe(ts);
49
+ });
50
+ test("throws for invalid UUID format", function () {
51
+ expect(function () {
52
+ return parseUUIDv7Timestamp("invalid");
53
+ }).toThrow("Invalid UUID format");
54
+ });
55
+ });
56
+ describe("createRandomUUIDv7", function () {
57
+ test("creates generator with custom now function", function () {
58
+ var currentTime = 1704067200000;
59
+ var generator = createRandomUUIDv7({
60
+ now: function () {
61
+ return currentTime;
62
+ }
63
+ });
64
+ var uuid1 = generator();
65
+ expect(parseUUIDv7Timestamp(uuid1)).toBe(currentTime);
66
+ currentTime = 1704067201000;
67
+ var uuid2 = generator();
68
+ expect(parseUUIDv7Timestamp(uuid2)).toBe(currentTime);
69
+ });
70
+ test("generator accepts explicit timestamp override", function () {
71
+ var generator = createRandomUUIDv7({
72
+ now: function () {
73
+ return 1000;
74
+ }
75
+ });
76
+ var uuid = generator(2000);
77
+ expect(parseUUIDv7Timestamp(uuid)).toBe(2000);
78
+ });
79
+ });
@@ -146,7 +146,7 @@ export function createFetchWith(param) {
146
146
  } : _param_onResponse;
147
147
  return function (urlOrRequest, init) {
148
148
  return _async_to_generator(function () {
149
- var url, req, nextFetch, _ref, res, _tmp;
149
+ var _ref, url, req, nextFetch, res, _tmp;
150
150
  return _ts_generator(this, function (_state) {
151
151
  switch (_state.label) {
152
152
  case 0:
package/lib/index.js CHANGED
@@ -77,9 +77,10 @@ export { sha1, sha256, sha384, sha512, hmac } from "./crypto/hashing.js";
77
77
  export { md5 } from "./crypto/md5.js";
78
78
  export { hex } from "./crypto/base.js";
79
79
  export { isULID, createULID, ulid, parseULID } from "./crypto/ulid.js";
80
+ export { randomUUIDv7, isUUIDv7, parseUUIDv7Timestamp, createRandomUUIDv7 } from "./crypto/randomUUIDv7.js";
80
81
  export { PEM } from "./crypto/pem/pem.js";
81
82
  // math
82
- export { createRandom } from "./maths/random.js";
83
+ export { createRandom, resolveRandom } from "./maths/random.js";
83
84
  export { clamp } from "./maths/clamp.js";
84
85
  // network
85
86
  export { createFetchWith, createFetchWithLogging, dumpResponse, dumpRequest, createFetchWithRetry } from "./fetch/index.js";
package/lib/io/dump.js CHANGED
@@ -2,8 +2,8 @@ import { ArrayBuffers } from "./ArrayBuffers.js";
2
2
  export function toHexDump(data) {
3
3
  var _ArrayBuffers_toHex_match;
4
4
  return Array.from((_ArrayBuffers_toHex_match = ArrayBuffers.toHex(data).match(/.{1,32}/g)) !== null && _ArrayBuffers_toHex_match !== void 0 ? _ArrayBuffers_toHex_match : []).map(function (v, i) {
5
- var idxPrefix = "".concat(i.toString(16).padStart(8, "0"), ":");
6
5
  var _v_match;
6
+ var idxPrefix = "".concat(i.toString(16).padStart(8, "0"), ":");
7
7
  var content = Array.from((_v_match = v.match(/.{4}/g)) !== null && _v_match !== void 0 ? _v_match : []).join(" ");
8
8
  var chars = Array.from(new Uint8Array(ArrayBuffers.fromHex(v).buffer)).map(function (v) {
9
9
  var c = String.fromCharCode(v);
@@ -75,9 +75,6 @@ function _is_native_reflect_construct() {
75
75
  }
76
76
  import { expect, test } from "vitest";
77
77
  import { mixin } from "./mixin.js";
78
- function Ent() {
79
- return function () { };
80
- }
81
78
  test("mixin", function () {
82
79
  // @Ent()
83
80
  var User = /*#__PURE__*/ function (_mixin) {
@@ -1,90 +1,11 @@
1
- function _array_like_to_array(arr, len) {
2
- if (len == null || len > arr.length)
3
- len = arr.length;
4
- for (var i = 0, arr2 = new Array(len); i < len; i++)
5
- arr2[i] = arr[i];
6
- return arr2;
7
- }
8
- function _array_with_holes(arr) {
9
- if (Array.isArray(arr))
10
- return arr;
11
- }
12
- function _iterable_to_array_limit(arr, i) {
13
- var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
14
- if (_i == null)
15
- return;
16
- var _arr = [];
17
- var _n = true;
18
- var _d = false;
19
- var _s, _e;
20
- try {
21
- for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
22
- _arr.push(_s.value);
23
- if (i && _arr.length === i)
24
- break;
25
- }
26
- }
27
- catch (err) {
28
- _d = true;
29
- _e = err;
30
- }
31
- finally {
32
- try {
33
- if (!_n && _i["return"] != null)
34
- _i["return"]();
35
- }
36
- finally {
37
- if (_d)
38
- throw _e;
39
- }
40
- }
41
- return _arr;
42
- }
43
- function _non_iterable_rest() {
44
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
45
- }
46
- function _sliced_to_array(arr, i) {
47
- return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
48
- }
49
- function _type_of(obj) {
50
- "@swc/helpers - typeof";
51
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
52
- }
53
- function _unsupported_iterable_to_array(o, minLen) {
54
- if (!o)
55
- return;
56
- if (typeof o === "string")
57
- return _array_like_to_array(o, minLen);
58
- var n = Object.prototype.toString.call(o).slice(8, -1);
59
- if (n === "Object" && o.constructor)
60
- n = o.constructor.name;
61
- if (n === "Map" || n === "Set")
62
- return Array.from(n);
63
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
64
- return _array_like_to_array(o, minLen);
65
- }
66
- import { isDefined } from "../langs/isDefined.js";
67
- import { isNil } from "../langs/isNil.js";
68
- export function clamp(value) {
69
- for (var _len = arguments.length, o = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
70
- o[_key - 1] = arguments[_key];
71
- }
72
- var min, max, def;
73
- if (o.length === 1 && o[0] && _type_of(o[0]) === "object") {
74
- var ref, ref1;
75
- ref = o[0], min = ref.min, max = ref.max, ref1 = ref.default, def = ref1 === void 0 ? min : ref1, ref;
76
- }
77
- else {
78
- var ref2, ref3;
79
- ref2 = _sliced_to_array(o, 3), min = ref2[0], max = ref2[1], ref3 = ref2[2], def = ref3 === void 0 ? min : ref3, ref2;
80
- }
81
- if (isNil(value)) {
82
- return def;
1
+ export function clamp(value, min, max, def) {
2
+ if (value == null) {
3
+ return def !== null && def !== void 0 ? def : min;
83
4
  }
84
- if (isDefined(min) && value < min) {
5
+ if (min != null && value < min) {
85
6
  return min;
86
7
  }
87
- if (isDefined(max) && value > max) {
8
+ if (max != null && value > max) {
88
9
  return max;
89
10
  }
90
11
  return value;
@@ -63,6 +63,7 @@ import { expect, test } from "vitest";
63
63
  import { clamp } from "./clamp.js";
64
64
  test("clamp", function () {
65
65
  for (var _i = 0, _iter = [
66
+ // 基本用法
66
67
  [
67
68
  [
68
69
  null,
@@ -116,68 +117,65 @@ test("clamp", function () {
116
117
  ],
117
118
  [
118
119
  [
119
- 11,
120
- {
121
- min: 1,
122
- max: 10
123
- }
120
+ 0,
121
+ 1,
122
+ 10,
123
+ 5
124
124
  ],
125
- 10
125
+ 1
126
126
  ],
127
+ // 只限制 min
127
128
  [
128
129
  [
129
- null,
130
- {
131
- min: 1,
132
- max: 10
133
- }
130
+ 1,
131
+ 2,
132
+ undefined
134
133
  ],
135
- 1
134
+ 2
136
135
  ],
137
136
  [
138
137
  [
139
- null,
140
- {
141
- min: 1,
142
- max: 10,
143
- default: 5
144
- }
138
+ 5,
139
+ 2,
140
+ undefined
145
141
  ],
146
142
  5
147
143
  ],
144
+ // 只限制 max
148
145
  [
149
146
  [
150
- 2n,
151
147
  1,
152
- 10,
153
- 5
148
+ undefined,
149
+ 0
154
150
  ],
155
- 2n
151
+ 0
156
152
  ],
157
153
  [
158
154
  [
159
- 1,
160
- 2
155
+ 5,
156
+ undefined,
157
+ 10
161
158
  ],
162
- 2
159
+ 5
163
160
  ],
161
+ // BigInt
164
162
  [
165
163
  [
166
- 1,
167
- undefined,
168
- 0
164
+ 2n,
165
+ 1n,
166
+ 10n,
167
+ 5n
169
168
  ],
170
- 0
171
- ],
172
- [
173
- [],
174
- undefined
169
+ 2n
175
170
  ],
176
171
  [
177
172
  [
178
- null
173
+ 11n,
174
+ 1n,
175
+ 10n,
176
+ 5n
179
177
  ],
180
- undefined
178
+ 10n
181
179
  ]
182
180
  ]; _i < _iter.length; _i++) {
183
181
  var _iter__i = _sliced_to_array(_iter[_i], 2), a = _iter__i[0], b = _iter__i[1];