@wener/utils 1.1.12 → 1.1.14
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/dist/LICENSE.txt +1 -135
- package/dist/cjs/globalThis-ee7c1669.js +2 -0
- package/dist/cjs/globalThis-ee7c1669.js.map +1 -0
- package/dist/cjs/index.cjs +2 -2
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/server.cjs +1 -1
- package/dist/cjs/server.cjs.map +1 -1
- package/dist/esm/globalThis-000611c3.js +2 -0
- package/dist/esm/globalThis-000611c3.js.map +1 -0
- package/dist/esm/index.js +4 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/server.js +1 -1
- package/dist/esm/server.js.map +1 -1
- package/dist/system/globalThis-7bba6592.js +2 -0
- package/dist/system/globalThis-7bba6592.js.map +1 -0
- package/dist/system/index.js +4 -4
- package/dist/system/index.js.map +1 -1
- package/dist/system/server.js +1 -1
- package/dist/system/server.js.map +1 -1
- package/lib/asyncs/timeout.js +3 -1
- package/lib/asyncs/timeout.js.map +1 -1
- package/lib/browsers/copy.js.map +1 -1
- package/lib/browsers/download.js +3 -1
- package/lib/browsers/download.js.map +1 -1
- package/lib/browsers/loaders.js +6 -2
- package/lib/browsers/loaders.js.map +1 -1
- package/lib/crypto/getRandomValues.js +1 -0
- package/lib/crypto/getRandomValues.js.map +1 -1
- package/lib/crypto/randomUUID.js +2 -0
- package/lib/crypto/randomUUID.js.map +1 -1
- package/lib/i18n/createTranslate.js +2 -1
- package/lib/i18n/createTranslate.js.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/io/ArrayBuffers.js +1 -0
- package/lib/io/ArrayBuffers.js.map +1 -1
- package/lib/io/isTransferable.js +2 -0
- package/lib/io/isTransferable.js.map +1 -1
- package/lib/isomorphics/getGlobalThis.js.map +1 -1
- package/lib/isomorphics/globalThis.js +6 -0
- package/lib/isomorphics/globalThis.js.map +1 -0
- package/lib/isomorphics/structuredClone.js +1 -0
- package/lib/isomorphics/structuredClone.js.map +1 -1
- package/lib/logging/createLogger.js +15 -5
- package/lib/logging/createLogger.js.map +1 -1
- package/lib/server.js +3 -1
- package/lib/server.js.map +1 -1
- package/lib/servers/createFetchWithProxy.js +13 -0
- package/lib/servers/createFetchWithProxy.js.map +1 -0
- package/lib/servers/{createProxyFetch.js → createFetchWithProxyByNodeFetch.js} +8 -3
- package/lib/servers/createFetchWithProxyByNodeFetch.js.map +1 -0
- package/lib/servers/createFetchWithProxyByUndici.js +28 -0
- package/lib/servers/createFetchWithProxyByUndici.js.map +1 -0
- package/lib/servers/polyfillCrypto.js +2 -0
- package/lib/servers/polyfillCrypto.js.map +1 -1
- package/lib/servers/polyfillJsDom.js +2 -0
- package/lib/servers/polyfillJsDom.js.map +1 -1
- package/package.json +20 -28
- package/src/asyncs/createLazyPromise.test.ts +21 -21
- package/src/asyncs/timeout.ts +1 -1
- package/src/browsers/copy.ts +1 -1
- package/src/browsers/download.ts +1 -1
- package/src/browsers/loaders.ts +2 -2
- package/src/crypto/getRandomValues.ts +1 -0
- package/src/crypto/hashing.test.ts +10 -12
- package/src/crypto/pem/__snapshots__/pem.test.ts.snap +18 -0
- package/src/crypto/pem/pem.test.ts +14 -17
- package/src/crypto/randomUUID.ts +2 -0
- package/src/crypto/ulid.test.ts +9 -9
- package/src/i18n/createTranslate.test.ts +76 -88
- package/src/i18n/createTranslate.ts +2 -1
- package/src/index.ts +1 -0
- package/src/io/ArrayBuffer.test-d.ts +4 -2
- package/src/io/ArrayBuffers.base64.test.ts +17 -17
- package/src/io/ArrayBuffers.test.ts +8 -8
- package/src/io/ArrayBuffers.ts +1 -0
- package/src/io/Buffer.test.ts +4 -4
- package/src/io/isBuffer.test.ts +4 -4
- package/src/io/isTransferable.test.ts +7 -6
- package/src/io/isTransferable.ts +2 -0
- package/src/isomorphics/getGlobalThis.ts +3 -1
- package/src/isomorphics/globalThis.ts +3 -0
- package/src/isomorphics/structuredClone.test.ts +4 -4
- package/src/isomorphics/structuredClone.ts +1 -0
- package/src/langs/deepEqual.test.ts +4 -4
- package/src/langs/langs.test.ts +3 -3
- package/src/libs/ms.test.ts +311 -0
- package/src/logging/createLogger.ts +5 -5
- package/src/logging/logger.test.ts +8 -8
- package/src/modules/parseModuleId.test.ts +3 -3
- package/src/objects/get.test-d.ts +30 -22
- package/src/objects/parseObjectPath.test.ts +3 -3
- package/src/objects/set.test.ts +98 -117
- package/src/server.ts +3 -1
- package/src/servers/createFetchWithLogger.ts +75 -0
- package/src/servers/createFetchWithProxy.ts +12 -0
- package/src/servers/{createProxyFetch.ts → createFetchWithProxyByNodeFetch.ts} +6 -2
- package/src/servers/createFetchWithProxyByUndici.ts +36 -0
- package/src/servers/polyfillBrowser.test.ts +9 -9
- package/src/servers/polyfillCrypto.ts +3 -1
- package/src/servers/polyfillJsDom.ts +1 -0
- package/src/servers/polyfillWebSocket.ts +1 -0
- package/src/strings/renderTemplate.test.ts +6 -9
- package/src/validations/parseTimestamp.test.ts +4 -4
- package/dist/cjs/index-da9513d6.js +0 -13
- package/dist/cjs/index-da9513d6.js.map +0 -1
- package/dist/esm/index-c696799a.js +0 -13
- package/dist/esm/index-c696799a.js.map +0 -1
- package/dist/system/index-2dfef0f3.js +0 -13
- package/dist/system/index-2dfef0f3.js.map +0 -1
- package/lib/servers/createProxyFetch.js.map +0 -1
- package/src/crypto/pem/pem.test.ts.md +0 -24
- package/src/crypto/pem/pem.test.ts.snap +0 -0
- package/src/objects/get.test.ts +0 -63
|
@@ -1,80 +1,83 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { assert, test } from 'vitest';
|
|
2
2
|
import { createTranslate } from './createTranslate';
|
|
3
3
|
|
|
4
|
-
test('exports', (
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
test('exports', () => {
|
|
5
|
+
assert.equal(typeof createTranslate, 'function', 'exports a function');
|
|
6
|
+
|
|
7
|
+
let out = createTranslate();
|
|
8
|
+
assert.equal(typeof out, 'object', 'returns an object');
|
|
9
|
+
assert.equal(typeof out.t, 'function', '~> has "t" function');
|
|
10
|
+
assert.equal(typeof out.dict, 'function', '~> has "dict" function');
|
|
11
|
+
assert.equal(typeof out.locale, 'function', '~> has "locale" function');
|
|
10
12
|
});
|
|
11
13
|
|
|
12
|
-
test('usage', (
|
|
13
|
-
|
|
14
|
+
test('usage', () => {
|
|
15
|
+
let ctx = createTranslate({
|
|
14
16
|
en: { hello: 'Hello, {{name}}!' },
|
|
15
17
|
es: { hello: 'Hola {{name}}!' },
|
|
16
18
|
pt: { foo: 'foo {{name}}~!' },
|
|
17
19
|
});
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
assert.deepEqual(ctx.dict('en'), { hello: 'Hello, {{name}}!' });
|
|
22
|
+
|
|
23
|
+
assert.equal(ctx.dict('foobar'), undefined);
|
|
20
24
|
|
|
21
|
-
|
|
25
|
+
let foo = ctx.t('hello');
|
|
26
|
+
assert.equal(foo, '', '~> "" w/o locale');
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
t.is(foo, '', '~> "" w/o locale');
|
|
28
|
+
assert.equal(ctx.locale('en'), 'en', '>>> ctx.locale()');
|
|
25
29
|
|
|
26
|
-
|
|
30
|
+
assert.equal(ctx.locale(), 'en');
|
|
27
31
|
|
|
28
|
-
|
|
32
|
+
let bar = ctx.t('hello');
|
|
29
33
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
t.is(bar, 'Hello, !', '~> interpolations empty if missing param');
|
|
34
|
+
assert.notEqual(bar, '', '(en) found "hello" key');
|
|
35
|
+
assert.equal(bar, 'Hello, !', '~> interpolations empty if missing param');
|
|
33
36
|
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
let baz = ctx.t('hello', { name: 'world' });
|
|
38
|
+
assert.equal(baz, 'Hello, world!', '~> interpolations successful');
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
let bat = ctx.t('hello', { name: 'world' }, 'es');
|
|
41
|
+
assert.notEqual(bat, '', '(es) found "hello" key');
|
|
42
|
+
assert.equal(bat, 'Hola world!', '~> success');
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
assert.equal(ctx.t('hello', { name: 'world' }, 'pt'), '', '(pt) did NOT find "hello" key');
|
|
42
45
|
|
|
43
|
-
|
|
46
|
+
assert.equal(ctx.dict('pt', { hello: 'Oí {{name}}!' }), undefined, '>>> ctx.set()');
|
|
44
47
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
let quz = ctx.t('hello', { name: 'world' }, 'pt');
|
|
49
|
+
assert.notEqual(quz, '', '(pt) found "hello" key');
|
|
50
|
+
assert.equal(quz, 'Oí world!', '~> success');
|
|
48
51
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
+
let qut = ctx.t('foo', { name: 'bar' }, 'pt');
|
|
53
|
+
assert.notEqual(qut, '', '(pt) found "foo" key');
|
|
54
|
+
assert.equal(qut, 'foo bar~!', '~> success');
|
|
52
55
|
|
|
53
|
-
|
|
56
|
+
assert.equal(ctx.locale('es'), 'es', '>>> ctx.locale()');
|
|
54
57
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
assert.equal(ctx.locale(), 'es');
|
|
59
|
+
assert.equal(ctx.locale(''), 'es');
|
|
60
|
+
assert.equal(ctx.locale(false as any), 'es');
|
|
61
|
+
assert.equal(ctx.locale(null as any), 'es');
|
|
62
|
+
assert.equal(ctx.locale(0 as any), 'es');
|
|
60
63
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
+
let qux = ctx.t('hello', { name: 'default' });
|
|
65
|
+
assert.notEqual(qux, '', '(es) found "hello" key');
|
|
66
|
+
assert.equal(qux, 'Hola default!', '~> success');
|
|
64
67
|
|
|
65
|
-
|
|
68
|
+
assert.equal(ctx.t('hello', { name: 'world' }, 'de'), '', '(de) did NOT find "hello" key');
|
|
66
69
|
|
|
67
|
-
|
|
70
|
+
assert.equal(ctx.dict('de', { hello: 'Hallo {{name}}!' }), undefined, '>>> ctx.set(de)');
|
|
68
71
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
+
let qar = ctx.t('hello', { name: 'world' }, 'de');
|
|
73
|
+
assert.notEqual(qar, '', '(de) found "hello" key');
|
|
74
|
+
assert.equal(qar, 'Hallo world!', '~> success');
|
|
72
75
|
});
|
|
73
76
|
|
|
74
|
-
test('functional', (
|
|
75
|
-
|
|
77
|
+
test('functional', () => {
|
|
78
|
+
let ctx = createTranslate({
|
|
76
79
|
en: {
|
|
77
|
-
hello(value:
|
|
80
|
+
hello(value: string) {
|
|
78
81
|
return `hello ${value || 'stranger'}~!`;
|
|
79
82
|
},
|
|
80
83
|
},
|
|
@@ -82,18 +85,18 @@ test('functional', (t) => {
|
|
|
82
85
|
|
|
83
86
|
ctx.locale('en');
|
|
84
87
|
|
|
85
|
-
|
|
86
|
-
|
|
88
|
+
let foo = ctx.t('hello');
|
|
89
|
+
assert.equal(foo, 'hello stranger~!', '~> called function w/o param');
|
|
87
90
|
|
|
88
|
-
|
|
89
|
-
|
|
91
|
+
let bar = ctx.t('hello', 'world' as any);
|
|
92
|
+
assert.equal(bar, 'hello world~!', '~> called function w/ param (string)');
|
|
90
93
|
|
|
91
|
-
|
|
92
|
-
|
|
94
|
+
let baz = ctx.t('hello', [1, 2, 3]);
|
|
95
|
+
assert.equal(baz, 'hello 1,2,3~!', '~> called function w/ param (array)');
|
|
93
96
|
});
|
|
94
97
|
|
|
95
|
-
test('nested', (
|
|
96
|
-
|
|
98
|
+
test('nested', () => {
|
|
99
|
+
let ctx = createTranslate({
|
|
97
100
|
en: {
|
|
98
101
|
fruits: {
|
|
99
102
|
apple: 'apple',
|
|
@@ -111,22 +114,22 @@ test('nested', (t) => {
|
|
|
111
114
|
});
|
|
112
115
|
|
|
113
116
|
ctx.locale('en');
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
assert.equal(ctx.t('fruits.apple'), 'apple', '(en) fruits.apple');
|
|
118
|
+
assert.equal(ctx.t('fruits.orange'), 'orange', '(en) fruits.orange');
|
|
119
|
+
assert.equal(ctx.t(['fruits', 'grape']), 'grape', '(en) ["fruits","grape"]');
|
|
120
|
+
assert.equal(ctx.t('fruits.404'), '', '(en) fruits.404 ~> ""');
|
|
121
|
+
assert.equal(ctx.t('error.404'), '', '(en) error.404 ~> ""');
|
|
119
122
|
|
|
120
123
|
ctx.locale('es');
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
assert.equal(ctx.t('fruits.apple'), 'manzana', '(es) fruits.apple');
|
|
125
|
+
assert.equal(ctx.t('fruits.orange'), 'naranja', '(es) fruits.orange');
|
|
126
|
+
assert.equal(ctx.t(['fruits', 'grape']), 'uva', '(es) ["fruits","grape"]');
|
|
127
|
+
assert.equal(ctx.t('fruits.404'), '', '(es) fruits.404 ~> ""');
|
|
128
|
+
assert.equal(ctx.t('error.404'), '', '(es) error.404 ~> ""');
|
|
126
129
|
});
|
|
127
130
|
|
|
128
|
-
test('arrays', (
|
|
129
|
-
|
|
131
|
+
test('arrays', () => {
|
|
132
|
+
let ctx = createTranslate({
|
|
130
133
|
en: {
|
|
131
134
|
foo: '{{0}} + {{1}} = {{2}}',
|
|
132
135
|
bar: [
|
|
@@ -139,32 +142,17 @@ test('arrays', (t) => {
|
|
|
139
142
|
|
|
140
143
|
ctx.locale('en');
|
|
141
144
|
|
|
142
|
-
|
|
145
|
+
assert.equal(ctx.t('foo', [1, 2, 3]), '1 + 2 = 3', '~> foo');
|
|
143
146
|
|
|
144
|
-
|
|
147
|
+
assert.equal(ctx.t('bar.0.baz', { colors: ['red', 'blue'] }), 'roses are red, violets are blue', '~> bar.0.baz');
|
|
145
148
|
});
|
|
146
149
|
|
|
147
|
-
test('invalid value', (
|
|
148
|
-
|
|
150
|
+
test('invalid value', () => {
|
|
151
|
+
let ctx = createTranslate({
|
|
149
152
|
en: {
|
|
150
153
|
foo: ['bar'],
|
|
151
154
|
},
|
|
152
155
|
});
|
|
153
156
|
|
|
154
|
-
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
test('fallback', (t) => {
|
|
158
|
-
const ctx = createTranslate({
|
|
159
|
-
en: {
|
|
160
|
-
a: 'a',
|
|
161
|
-
},
|
|
162
|
-
'en-US': {
|
|
163
|
-
a: 'a-US',
|
|
164
|
-
},
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
// t.deepEqual(ctx.t('a', undefined, 'en'), 'a');
|
|
168
|
-
// t.deepEqual(ctx.t('a', undefined, 'en-US'), 'a-US');
|
|
169
|
-
t.deepEqual(ctx.t('a', undefined, 'en-UK'), 'a');
|
|
157
|
+
assert.deepEqual(ctx.t('foo', null as never, 'en'), ['bar'] as any);
|
|
170
158
|
});
|
|
@@ -53,11 +53,12 @@ export function createTranslate<T extends object>(obj?: Record<string, T>): Tran
|
|
|
53
53
|
}
|
|
54
54
|
if (process.env.NODE_ENV === 'development') {
|
|
55
55
|
if (val == null) {
|
|
56
|
-
|
|
56
|
+
console.error(
|
|
57
57
|
`[Translate] Missing the "${[].concat(key as any).join('.')}" key within the "${
|
|
58
58
|
lang || locale
|
|
59
59
|
}" dictionary`,
|
|
60
60
|
);
|
|
61
|
+
return;
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
if (typeof val === 'function') return val(params);
|
package/src/index.ts
CHANGED
|
@@ -72,6 +72,7 @@ export { getFileFromDataTransfer } from './browsers/getFileFromDataTransfer';
|
|
|
72
72
|
|
|
73
73
|
// polyfills
|
|
74
74
|
export { getGlobalThis } from './isomorphics/getGlobalThis';
|
|
75
|
+
export { globalThis } from './isomorphics/globalThis';
|
|
75
76
|
export { structuredClone } from './isomorphics/structuredClone';
|
|
76
77
|
|
|
77
78
|
// crypto
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { expectTypeOf, test } from 'vitest';
|
|
2
2
|
import { ArrayBuffers } from './ArrayBuffers';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
test('my types work properly', () => {
|
|
5
|
+
expectTypeOf(ArrayBuffers.asView(Buffer, new Uint8Array())).toMatchTypeOf<Buffer>();
|
|
6
|
+
});
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { test, beforeAll, assert, expect } from 'vitest';
|
|
2
2
|
import { ArrayBuffers } from './ArrayBuffers';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
beforeAll(() => {
|
|
5
|
+
expect(ArrayBuffers.isNativeBufferAvailable()).toBeTruthy();
|
|
6
6
|
ArrayBuffers.setNativeBufferAllowed(false);
|
|
7
7
|
});
|
|
8
8
|
|
|
9
|
-
test('base64: ignore whitespace', function (
|
|
9
|
+
test('base64: ignore whitespace', function () {
|
|
10
10
|
const text = '\n YW9ldQ== ';
|
|
11
11
|
const buf = ArrayBuffers.from(text, 'base64');
|
|
12
|
-
|
|
12
|
+
expect(ArrayBuffers.toString(buf)).toBe('aoeu');
|
|
13
13
|
});
|
|
14
14
|
|
|
15
|
-
test('base64: strings without padding', function (
|
|
16
|
-
|
|
15
|
+
test('base64: strings without padding', function () {
|
|
16
|
+
expect(ArrayBuffers.toString(ArrayBuffers.from('YW9ldQ', 'base64'))).toBe('aoeu');
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
-
test('base64: newline in utf8 -- should not be an issue', function (
|
|
20
|
-
|
|
19
|
+
test('base64: newline in utf8 -- should not be an issue', function () {
|
|
20
|
+
assert.equal(
|
|
21
21
|
ArrayBuffers.toString(
|
|
22
22
|
ArrayBuffers.from('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK', 'base64'),
|
|
23
23
|
'utf8',
|
|
@@ -26,8 +26,8 @@ test('base64: newline in utf8 -- should not be an issue', function (t) {
|
|
|
26
26
|
);
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
-
test('base64: newline in base64 -- should get stripped', function (
|
|
30
|
-
|
|
29
|
+
test('base64: newline in base64 -- should get stripped', function () {
|
|
30
|
+
assert.equal(
|
|
31
31
|
ArrayBuffers.toString(
|
|
32
32
|
ArrayBuffers.from(
|
|
33
33
|
'LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\nICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt',
|
|
@@ -39,8 +39,8 @@ test('base64: newline in base64 -- should get stripped', function (t) {
|
|
|
39
39
|
);
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
test('base64: tab characters in base64 - should get stripped', function (
|
|
43
|
-
|
|
42
|
+
test('base64: tab characters in base64 - should get stripped', function () {
|
|
43
|
+
assert.equal(
|
|
44
44
|
ArrayBuffers.toString(
|
|
45
45
|
ArrayBuffers.from(
|
|
46
46
|
'LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\t\t\t\tICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt',
|
|
@@ -52,11 +52,11 @@ test('base64: tab characters in base64 - should get stripped', function (t) {
|
|
|
52
52
|
);
|
|
53
53
|
});
|
|
54
54
|
|
|
55
|
-
test('base64: invalid non-alphanumeric characters -- should be stripped', function (
|
|
56
|
-
|
|
55
|
+
test('base64: invalid non-alphanumeric characters -- should be stripped', function () {
|
|
56
|
+
expect(ArrayBuffers.toString(ArrayBuffers.from('!"#$%&\'()*,.:;<=>?@[\\]^`{|}~', 'base64'), 'utf8')).toBe('');
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
-
test('base64: high byte', function (
|
|
59
|
+
test('base64: high byte', function () {
|
|
60
60
|
const highByte = ArrayBuffers.from([128]);
|
|
61
|
-
|
|
61
|
+
assert.deepEqual(ArrayBuffers.alloc(1, ArrayBuffers.toString(highByte, 'base64'), 'base64'), highByte);
|
|
62
62
|
});
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { test, expect } from 'vitest';
|
|
2
2
|
import { ArrayBuffers } from './ArrayBuffers';
|
|
3
3
|
|
|
4
|
-
test('concat', (
|
|
4
|
+
test('concat', () => {
|
|
5
5
|
const check = (Type: any = Uint8Array, va = [1, 2, 3], vb = [4, 5, 6]) => {
|
|
6
6
|
const a = new Type(va);
|
|
7
7
|
const b = new Type(vb);
|
|
8
|
-
|
|
8
|
+
expect(ArrayBuffers.concat([a, b]), `should concat ${Type}`).toStrictEqual(new Type([...va, ...vb]).buffer);
|
|
9
9
|
};
|
|
10
10
|
check(Uint8Array);
|
|
11
11
|
check(Uint16Array, [0xff, 0xffff]);
|
|
12
12
|
check(Uint32Array, [0xfffffff]);
|
|
13
13
|
});
|
|
14
14
|
|
|
15
|
-
test('asView for Buffer', (
|
|
15
|
+
test('asView for Buffer', () => {
|
|
16
16
|
const buf = Buffer.from([1, 2, 3, 4, 5]);
|
|
17
|
-
|
|
17
|
+
expect(ArrayBuffers.asView(Buffer, buf)).toBe(buf);
|
|
18
18
|
const uint8 = ArrayBuffers.asView(Uint8Array, buf);
|
|
19
|
-
|
|
19
|
+
expect(ArrayBuffers.asView(Buffer, uint8).buffer).toBe(buf.buffer);
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
expect(uint8).toEqual(buf);
|
|
22
|
+
expect(ArrayBuffers.asView(Buffer, uint8)).toEqual(buf);
|
|
23
23
|
});
|
package/src/io/ArrayBuffers.ts
CHANGED
package/src/io/Buffer.test.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { test, expect } from 'vitest';
|
|
2
2
|
import { Buffer } from './Buffer';
|
|
3
3
|
import { isBuffer } from './isBuffer';
|
|
4
4
|
|
|
5
|
-
test('basic', (
|
|
5
|
+
test('basic', () => {
|
|
6
6
|
{
|
|
7
7
|
const buf = new Buffer(0);
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
expect(Buffer.isBuffer(buf)).toBeTruthy();
|
|
9
|
+
expect(isBuffer(buf)).toBeTruthy();
|
|
10
10
|
}
|
|
11
11
|
// const b = new Buffer(10)
|
|
12
12
|
// t.is(b.length,10)
|
package/src/io/isBuffer.test.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import test from 'ava';
|
|
2
1
|
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { expect, test } from 'vitest';
|
|
3
3
|
import { classOf } from '../langs/classOf';
|
|
4
4
|
import { isBuffer } from './isBuffer';
|
|
5
5
|
|
|
6
|
-
test('isBuffer', (
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
test('isBuffer', () => {
|
|
7
|
+
expect(isBuffer(Buffer.from(''))).toBeTruthy();
|
|
8
|
+
expect(classOf(Buffer)).toBe('Function');
|
|
9
9
|
});
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import test from 'ava';
|
|
2
1
|
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { expect, test } from 'vitest';
|
|
3
3
|
import { isTransferable } from './isTransferable';
|
|
4
4
|
|
|
5
|
-
test('isTransferable', (
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
test('isTransferable', () => {
|
|
6
|
+
expect(isTransferable(0)).toBeFalsy();
|
|
7
|
+
expect(isTransferable(Buffer.from(''))).toBeFalsy();
|
|
8
|
+
|
|
9
|
+
expect(new ArrayBuffer(0) instanceof ArrayBuffer).toBeTruthy();
|
|
10
|
+
expect(isTransferable(new ArrayBuffer(0))).toBeTruthy();
|
|
10
11
|
});
|
package/src/io/isTransferable.ts
CHANGED
|
@@ -5,6 +5,8 @@ declare const global: typeof globalThis;
|
|
|
5
5
|
*
|
|
6
6
|
* globalThis supported by ff 65, Chrome 71, Node 12, babel
|
|
7
7
|
*
|
|
8
|
+
* non-standard globalThis supported for Alipay Miniprogram
|
|
9
|
+
*
|
|
8
10
|
* @see https://caniuse.com/#search=globalThis
|
|
9
11
|
* @see https://v8.dev/features/globalthis
|
|
10
12
|
*/
|
|
@@ -13,6 +15,6 @@ export const getGlobalThis = (): typeof globalThis => {
|
|
|
13
15
|
if (typeof self !== 'undefined') return self;
|
|
14
16
|
if (typeof window !== 'undefined') return window;
|
|
15
17
|
if (typeof global !== 'undefined') return global as any;
|
|
16
|
-
|
|
18
|
+
if (typeof this !== 'undefined') return this as any;
|
|
17
19
|
throw new Error('Unable to locate global `this`');
|
|
18
20
|
};
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { expect, test } from 'vitest';
|
|
2
2
|
import { classOf } from '../langs/classOf';
|
|
3
3
|
import { _clone } from './structuredClone';
|
|
4
4
|
|
|
5
|
-
test('structuredClone', (
|
|
5
|
+
test('structuredClone', () => {
|
|
6
6
|
for (const [k, v] of [
|
|
7
7
|
['', ''],
|
|
8
8
|
[Number(1), 1],
|
|
9
9
|
]) {
|
|
10
10
|
const c = _clone(k);
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
expect(c).toEqual(v);
|
|
12
|
+
expect(classOf(c)).toBe(classOf(v));
|
|
13
13
|
}
|
|
14
14
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { expect, test } from 'vitest';
|
|
2
2
|
import { deepEqual } from './deepEqual';
|
|
3
3
|
|
|
4
|
-
test('deep equal', (
|
|
5
|
-
|
|
4
|
+
test('deep equal', () => {
|
|
5
|
+
expect(
|
|
6
6
|
deepEqual(
|
|
7
7
|
{
|
|
8
8
|
a: null,
|
|
@@ -13,5 +13,5 @@ test('deep equal', (t) => {
|
|
|
13
13
|
b: Infinity,
|
|
14
14
|
},
|
|
15
15
|
),
|
|
16
|
-
);
|
|
16
|
+
).toBeTruthy();
|
|
17
17
|
});
|
package/src/langs/langs.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import test from '
|
|
1
|
+
import { expect, test } from 'vitest';
|
|
2
2
|
import { classOf } from './classOf';
|
|
3
3
|
|
|
4
|
-
test('classOf', (
|
|
4
|
+
test('classOf', () => {
|
|
5
5
|
for (const [k, v] of [
|
|
6
6
|
[0, 'Number'],
|
|
7
7
|
['', 'String'],
|
|
@@ -18,6 +18,6 @@ test('classOf', (t) => {
|
|
|
18
18
|
[new DataView(new ArrayBuffer(0)), 'DataView'],
|
|
19
19
|
[new Int8Array(0), 'Int8Array'],
|
|
20
20
|
]) {
|
|
21
|
-
|
|
21
|
+
expect(classOf(k)).toBe(v);
|
|
22
22
|
}
|
|
23
23
|
});
|