@wener/utils 1.1.6 → 1.1.7
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 +3 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/server.js +1 -1
- package/dist/cjs/server.js.map +1 -1
- package/dist/esm/index.js +1 -1
- 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/index.js +1 -1
- 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/arrays/MaybeArray.js.map +1 -1
- package/lib/asyncs/createLazyPromise.js +2 -2
- package/lib/asyncs/createLazyPromise.js.map +1 -1
- package/lib/asyncs/timeout.js +1 -1
- package/lib/asyncs/timeout.js.map +1 -1
- package/lib/browsers/copy.js +2 -3
- package/lib/browsers/copy.js.map +1 -1
- package/lib/browsers/loaders.js +6 -11
- package/lib/browsers/loaders.js.map +1 -1
- package/lib/crypto/{hex.js → base.js} +1 -1
- package/lib/crypto/base.js.map +1 -0
- package/lib/crypto/hashing.js +4 -4
- package/lib/crypto/hashing.js.map +1 -1
- package/lib/crypto/randomUUID.js.map +1 -1
- package/lib/i18n/createTranslate.js.map +1 -1
- package/lib/index.js +2 -2
- package/lib/io/ArrayBuffers.js +164 -0
- package/lib/io/ArrayBuffers.js.map +1 -0
- package/lib/io/isBuffer.js +1 -1
- package/lib/io/isBuffer.js.map +1 -1
- package/lib/io/isTransferable.js.map +1 -1
- package/lib/isomorphics/structuredClone.js.map +1 -1
- package/lib/langs/deepEqual.js.map +1 -1
- package/lib/langs/shallowEqual.js.map +1 -1
- package/lib/logging/createNoopLogger.js.map +1 -1
- package/lib/modules/isModule.js.map +1 -1
- package/lib/modules/parseModuleId.js +7 -5
- package/lib/modules/parseModuleId.js.map +1 -1
- package/lib/objects/parseObjectPath.js.map +1 -1
- package/lib/objects/set.js.map +1 -1
- package/lib/server/polyfillBrowser.js.map +1 -1
- package/lib/server/polyfillFetch.js.map +1 -1
- package/lib/server/polyfillJsDom.js +1 -1
- package/lib/server/polyfillJsDom.js.map +1 -1
- package/lib/strings/formatBytes.js +1 -1
- package/lib/strings/formatBytes.js.map +1 -1
- package/lib/strings/renderTemplate.js +1 -1
- package/lib/strings/renderTemplate.js.map +1 -1
- package/package.json +10 -2
- package/src/arrays/MaybeArray.ts +1 -1
- package/src/asyncs/createLazyPromise.test.ts +4 -4
- package/src/asyncs/createLazyPromise.ts +2 -2
- package/src/asyncs/generatorOfStream.ts +1 -0
- package/src/asyncs/timeout.ts +1 -1
- package/src/browsers/copy.ts +6 -5
- package/src/browsers/loaders.ts +6 -11
- package/src/crypto/{hex.ts → base.ts} +3 -0
- package/src/crypto/hashing.test.ts +1 -1
- package/src/crypto/hashing.ts +4 -4
- package/src/crypto/randomUUID.ts +3 -0
- package/src/i18n/createTranslate.test.ts +18 -18
- package/src/i18n/createTranslate.ts +1 -1
- package/src/index.ts +3 -2
- package/src/io/AbstractEncoding.ts +21 -0
- package/src/io/ArrayBuffer.test-d.ts +4 -0
- package/src/io/ArrayBuffers.base64.test.ts +61 -0
- package/src/io/ArrayBuffers.test.ts +23 -0
- package/src/io/ArrayBuffers.ts +272 -0
- package/src/io/Buffer.ts +16 -0
- package/src/io/isBuffer.test.ts +2 -0
- package/src/io/isBuffer.ts +3 -8
- package/src/io/isTransferable.ts +1 -1
- package/src/isomorphics/structuredClone.test.ts +1 -1
- package/src/isomorphics/structuredClone.ts +9 -6
- package/src/langs/deepEqual.ts +1 -0
- package/src/langs/shallowEqual.ts +1 -1
- package/src/logging/Logger.ts +6 -0
- package/src/logging/createNoopLogger.ts +1 -1
- package/src/logging/logger.test.ts +3 -3
- package/src/modules/isModule.ts +3 -0
- package/src/modules/parseModuleId.test.ts +7 -2
- package/src/modules/parseModuleId.ts +15 -9
- package/src/objects/get.test-d.ts +51 -0
- package/src/objects/get.test.ts +2 -55
- package/src/objects/parseObjectPath.ts +4 -3
- package/src/objects/set.test.ts +32 -31
- package/src/objects/set.ts +2 -2
- package/src/server/polyfillBrowser.ts +8 -0
- package/src/server/polyfillFetch.ts +0 -1
- package/src/server/polyfillJsDom.ts +3 -2
- package/src/strings/formatBytes.ts +1 -1
- package/src/strings/renderTemplate.test.ts +1 -0
- package/src/strings/renderTemplate.ts +8 -5
- package/src/typedoc.ts +2 -0
- package/tsconfig.json +2 -1
- package/dist/cjs/_commonjsHelpers-dfec268f.js +0 -2
- package/dist/cjs/_commonjsHelpers-dfec268f.js.map +0 -1
- package/dist/cjs/api-7db97ae3.js +0 -1085
- package/dist/cjs/api-7db97ae3.js.map +0 -1
- package/dist/cjs/index-a6d1d653.js +0 -14
- package/dist/cjs/index-a6d1d653.js.map +0 -1
- package/dist/cjs/multipart-parser-141ed517.js +0 -3
- package/dist/cjs/multipart-parser-141ed517.js.map +0 -1
- package/dist/esm/_commonjsHelpers-28e086c5.js +0 -2
- package/dist/esm/_commonjsHelpers-28e086c5.js.map +0 -1
- package/dist/esm/api-3f555472.js +0 -1085
- package/dist/esm/api-3f555472.js.map +0 -1
- package/dist/esm/index-b50fef91.js +0 -14
- package/dist/esm/index-b50fef91.js.map +0 -1
- package/dist/esm/multipart-parser-5c1d6ee9.js +0 -3
- package/dist/esm/multipart-parser-5c1d6ee9.js.map +0 -1
- package/dist/system/_commonjsHelpers-07f370a7.js +0 -2
- package/dist/system/_commonjsHelpers-07f370a7.js.map +0 -1
- package/dist/system/api-dc50ebac.js +0 -1085
- package/dist/system/api-dc50ebac.js.map +0 -1
- package/dist/system/index-8f1807ba.js +0 -14
- package/dist/system/index-8f1807ba.js.map +0 -1
- package/dist/system/multipart-parser-53518ee9.js +0 -3
- package/dist/system/multipart-parser-53518ee9.js.map +0 -1
- package/lib/crypto/hex.js.map +0 -1
- package/lib/shim/urljoin.js +0 -51
- package/lib/shim/urljoin.js.map +0 -1
- package/src/shim/urljoin.test.ts +0 -6
- package/src/shim/urljoin.ts +0 -75
- package/src/types.d.ts +0 -7
|
@@ -3,9 +3,9 @@ import { createLazyPromise } from './createLazyPromise';
|
|
|
3
3
|
import { sleep } from './sleep';
|
|
4
4
|
|
|
5
5
|
test('basic', async (t) => {
|
|
6
|
-
|
|
6
|
+
const promise = createLazyPromise();
|
|
7
7
|
let r = 0;
|
|
8
|
-
promise.then((v) => (r = v));
|
|
8
|
+
void promise.then((v) => (r = v));
|
|
9
9
|
t.is(r, 0);
|
|
10
10
|
const { resolve, reject } = promise;
|
|
11
11
|
t.truthy(resolve);
|
|
@@ -17,7 +17,7 @@ test('basic', async (t) => {
|
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
test('manual resolve skip exec', async (t) => {
|
|
20
|
-
|
|
20
|
+
const promise = createLazyPromise(() => {
|
|
21
21
|
t.fail();
|
|
22
22
|
});
|
|
23
23
|
promise.resolve(-1);
|
|
@@ -26,7 +26,7 @@ test('manual resolve skip exec', async (t) => {
|
|
|
26
26
|
|
|
27
27
|
test('exec', async (t) => {
|
|
28
28
|
let r = 0;
|
|
29
|
-
|
|
29
|
+
const promise = createLazyPromise((resolve) => {
|
|
30
30
|
r++;
|
|
31
31
|
resolve(10);
|
|
32
32
|
});
|
|
@@ -18,7 +18,7 @@ export function createLazyPromise<T = any>(
|
|
|
18
18
|
throw new Error('pending reject');
|
|
19
19
|
},
|
|
20
20
|
};
|
|
21
|
-
|
|
21
|
+
const future = Object.assign(
|
|
22
22
|
new Promise<T>((resolve, reject) => {
|
|
23
23
|
holder.reject = reject;
|
|
24
24
|
holder.resolve = resolve;
|
|
@@ -52,7 +52,7 @@ export function createLazyPromise<T = any>(
|
|
|
52
52
|
like.catch = future.catch.bind(future);
|
|
53
53
|
if (future.finally) like.finally = future.finally;
|
|
54
54
|
|
|
55
|
-
Object.defineProperty(like, Symbol.species, {
|
|
55
|
+
void Object.defineProperty(like, Symbol.species, {
|
|
56
56
|
get() {
|
|
57
57
|
return Promise;
|
|
58
58
|
},
|
|
@@ -3,6 +3,7 @@ export function* generatorOfStream<T = any>(stream: any): IterableIterator<Promi
|
|
|
3
3
|
stream.on('end', () => {
|
|
4
4
|
done = true;
|
|
5
5
|
});
|
|
6
|
+
// eslint-disable-next-line no-unmodified-loop-condition
|
|
6
7
|
while (!done) {
|
|
7
8
|
yield new Promise((resolve, reject) => {
|
|
8
9
|
stream.once('data', resolve);
|
package/src/asyncs/timeout.ts
CHANGED
package/src/browsers/copy.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { MaybePromise } from '../asyncs/MaybePromise';
|
|
2
|
+
|
|
2
3
|
let _copy: (s: any) => void;
|
|
3
4
|
|
|
5
|
+
// https://gist.github.com/rproenca/64781c6a1329b48a455b645d361a9aa3
|
|
4
6
|
function initCopy() {
|
|
5
7
|
let textArea: HTMLTextAreaElement;
|
|
6
8
|
|
|
@@ -49,13 +51,12 @@ function initCopy() {
|
|
|
49
51
|
* Write text to clipboard
|
|
50
52
|
* @param content content
|
|
51
53
|
*/
|
|
52
|
-
export function copy(content: string) {
|
|
54
|
+
export function copy(content: string): MaybePromise<void> {
|
|
53
55
|
if (window.navigator?.clipboard?.writeText) {
|
|
54
|
-
window.navigator.clipboard.writeText(content);
|
|
55
|
-
return;
|
|
56
|
+
return window.navigator.clipboard.writeText(content);
|
|
56
57
|
}
|
|
57
58
|
if (!_copy) {
|
|
58
59
|
initCopy();
|
|
59
60
|
}
|
|
60
|
-
_copy(content);
|
|
61
|
+
return _copy(content);
|
|
61
62
|
}
|
package/src/browsers/loaders.ts
CHANGED
|
@@ -26,21 +26,16 @@ export function loadScripts(
|
|
|
26
26
|
options?: { attributes: Record<string, string> },
|
|
27
27
|
): Promise<HTMLScriptElement | HTMLScriptElement[]> {
|
|
28
28
|
if (Array.isArray(src)) {
|
|
29
|
-
return
|
|
29
|
+
return Promise.resolve().then(async () => {
|
|
30
30
|
const all = [];
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
all.push(await loadScripts(s));
|
|
34
|
-
}
|
|
35
|
-
} catch (e) {
|
|
36
|
-
reject(e);
|
|
37
|
-
return;
|
|
31
|
+
for (const s of src) {
|
|
32
|
+
all.push(await loadScripts(s));
|
|
38
33
|
}
|
|
39
|
-
|
|
34
|
+
return all;
|
|
40
35
|
});
|
|
41
36
|
}
|
|
42
37
|
// todo quote ?
|
|
43
|
-
|
|
38
|
+
const $ele = document.querySelector(`script[src="${src}"]`) as HTMLScriptElement;
|
|
44
39
|
if ($ele) {
|
|
45
40
|
return Promise.resolve($ele);
|
|
46
41
|
}
|
|
@@ -52,7 +47,7 @@ export function loadScripts(
|
|
|
52
47
|
}
|
|
53
48
|
|
|
54
49
|
export function loadStyles(href: string, options?: { attributes: Record<string, string> }): Promise<HTMLLinkElement> {
|
|
55
|
-
|
|
50
|
+
const $ele = document.querySelector(`link[href="${href}"]`) as HTMLLinkElement;
|
|
56
51
|
if ($ele) {
|
|
57
52
|
return Promise.resolve($ele);
|
|
58
53
|
}
|
|
@@ -2,7 +2,7 @@ import test from 'ava';
|
|
|
2
2
|
import { polyfillCrypto } from '../server/polyfillCrypto';
|
|
3
3
|
import { isUUID } from '../validations/isUUID';
|
|
4
4
|
import { sha1, sha256, sha384, sha512 } from './hashing';
|
|
5
|
-
import { hex } from './
|
|
5
|
+
import { hex } from './base';
|
|
6
6
|
import { _randomUUID } from './randomUUID';
|
|
7
7
|
|
|
8
8
|
test.before(async () => {
|
package/src/crypto/hashing.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
export
|
|
1
|
+
export function sha1(s: string | BufferSource) {
|
|
2
2
|
return digestOf('SHA-1', s);
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
-
export
|
|
5
|
+
export function sha256(s: string | BufferSource) {
|
|
6
6
|
return digestOf('SHA-256', s);
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export
|
|
9
|
+
export function sha384(s: string | BufferSource) {
|
|
10
10
|
return digestOf('SHA-384', s);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export
|
|
13
|
+
export function sha512(s: string | BufferSource) {
|
|
14
14
|
return digestOf('SHA-512', s);
|
|
15
15
|
}
|
|
16
16
|
|
package/src/crypto/randomUUID.ts
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export const randomUUID: () => string = globalThis.crypto?.randomUUID || _randomUUID;
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
6
9
|
export function _randomUUID() {
|
|
7
10
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
8
11
|
const r = (Math.random() * 16) | 0;
|
|
@@ -2,7 +2,7 @@ import test from 'ava';
|
|
|
2
2
|
import { createTranslate } from './createTranslate';
|
|
3
3
|
|
|
4
4
|
test('exports', (t) => {
|
|
5
|
-
|
|
5
|
+
const out = createTranslate();
|
|
6
6
|
t.is(typeof out, 'object', 'returns an object');
|
|
7
7
|
t.is(typeof out.t, 'function', '~> has "t" function');
|
|
8
8
|
t.is(typeof out.dict, 'function', '~> has "set" function');
|
|
@@ -10,7 +10,7 @@ test('exports', (t) => {
|
|
|
10
10
|
});
|
|
11
11
|
|
|
12
12
|
test('usage', (t) => {
|
|
13
|
-
|
|
13
|
+
const ctx = createTranslate({
|
|
14
14
|
en: { hello: 'Hello, {{name}}!' },
|
|
15
15
|
es: { hello: 'Hola {{name}}!' },
|
|
16
16
|
pt: { foo: 'foo {{name}}~!' },
|
|
@@ -18,23 +18,23 @@ test('usage', (t) => {
|
|
|
18
18
|
|
|
19
19
|
t.deepEqual(ctx.dict('en'), { hello: 'Hello, {{name}}!' });
|
|
20
20
|
|
|
21
|
-
t.
|
|
21
|
+
t.true(ctx.dict('foobar') === undefined);
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
const foo = ctx.t('hello');
|
|
24
24
|
t.is(foo, '', '~> "" w/o locale');
|
|
25
25
|
|
|
26
26
|
t.is(ctx.locale('en'), 'en', '>>> ctx.locale()');
|
|
27
27
|
|
|
28
28
|
t.is(ctx.locale(), 'en');
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
const bar = ctx.t('hello');
|
|
31
31
|
t.not(bar, '', '(en) found "hello" key');
|
|
32
32
|
t.is(bar, 'Hello, !', '~> interpolations empty if missing param');
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
const baz = ctx.t('hello', { name: 'world' });
|
|
35
35
|
t.is(baz, 'Hello, world!', '~> interpolations successful');
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
const bat = ctx.t('hello', { name: 'world' }, 'es');
|
|
38
38
|
t.not(bat, '', '(es) found "hello" key');
|
|
39
39
|
t.is(bat, 'Hola world!', '~> success');
|
|
40
40
|
|
|
@@ -42,11 +42,11 @@ test('usage', (t) => {
|
|
|
42
42
|
|
|
43
43
|
t.is(ctx.dict('pt', { hello: 'Oí {{name}}!' }), undefined, '>>> ctx.dict()');
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
const quz = ctx.t('hello', { name: 'world' }, 'pt');
|
|
46
46
|
t.not(quz, '', '(pt) found "hello" key');
|
|
47
47
|
t.is(quz, 'Oí world!', '~> success');
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
const qut = ctx.t('foo', { name: 'bar' }, 'pt');
|
|
50
50
|
t.not(qut, '', '(pt) found "foo" key');
|
|
51
51
|
t.is(qut, 'foo bar~!', '~> success');
|
|
52
52
|
|
|
@@ -58,7 +58,7 @@ test('usage', (t) => {
|
|
|
58
58
|
t.is(ctx.locale(null as any), 'es');
|
|
59
59
|
t.is(ctx.locale(0 as any), 'es');
|
|
60
60
|
|
|
61
|
-
|
|
61
|
+
const qux = ctx.t('hello', { name: 'default' });
|
|
62
62
|
t.not(qux, '', '(es) found "hello" key');
|
|
63
63
|
t.is(qux, 'Hola default!', '~> success');
|
|
64
64
|
|
|
@@ -66,13 +66,13 @@ test('usage', (t) => {
|
|
|
66
66
|
|
|
67
67
|
t.is(ctx.dict('de', { hello: 'Hallo {{name}}!' }), undefined, '>>> ctx.dict(de)');
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
const qar = ctx.t('hello', { name: 'world' }, 'de');
|
|
70
70
|
t.not(qar, '', '(de) found "hello" key');
|
|
71
71
|
t.is(qar, 'Hallo world!', '~> success');
|
|
72
72
|
});
|
|
73
73
|
|
|
74
74
|
test('functional', (t) => {
|
|
75
|
-
|
|
75
|
+
const ctx = createTranslate({
|
|
76
76
|
en: {
|
|
77
77
|
hello(value: any) {
|
|
78
78
|
return `hello ${value || 'stranger'}~!`;
|
|
@@ -82,18 +82,18 @@ test('functional', (t) => {
|
|
|
82
82
|
|
|
83
83
|
ctx.locale('en');
|
|
84
84
|
|
|
85
|
-
|
|
85
|
+
const foo = ctx.t('hello');
|
|
86
86
|
t.is(foo, 'hello stranger~!', '~> called function w/o param');
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
const bar = ctx.t('hello', 'world' as any);
|
|
89
89
|
t.is(bar, 'hello world~!', '~> called function w/ param (string)');
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
const baz = ctx.t('hello', [1, 2, 3]);
|
|
92
92
|
t.is(baz, 'hello 1,2,3~!', '~> called function w/ param (array)');
|
|
93
93
|
});
|
|
94
94
|
|
|
95
95
|
test('nested', (t) => {
|
|
96
|
-
|
|
96
|
+
const ctx = createTranslate({
|
|
97
97
|
en: {
|
|
98
98
|
fruits: {
|
|
99
99
|
apple: 'apple',
|
|
@@ -126,7 +126,7 @@ test('nested', (t) => {
|
|
|
126
126
|
});
|
|
127
127
|
|
|
128
128
|
test('arrays', (t) => {
|
|
129
|
-
|
|
129
|
+
const ctx = createTranslate({
|
|
130
130
|
en: {
|
|
131
131
|
foo: '{{0}} + {{1}} = {{2}}',
|
|
132
132
|
bar: [
|
|
@@ -145,7 +145,7 @@ test('arrays', (t) => {
|
|
|
145
145
|
});
|
|
146
146
|
|
|
147
147
|
test('invalid value', (t) => {
|
|
148
|
-
|
|
148
|
+
const ctx = createTranslate({
|
|
149
149
|
en: {
|
|
150
150
|
foo: ['bar'],
|
|
151
151
|
},
|
|
@@ -45,7 +45,7 @@ export function createTranslate<T extends object>(obj?: Record<string, T>): Tran
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
if (typeof val === 'function') return val(params);
|
|
48
|
-
if (typeof val === 'string') return renderTemplate(val, params
|
|
48
|
+
if (typeof val === 'string') return renderTemplate(val, params, 'common');
|
|
49
49
|
return val;
|
|
50
50
|
},
|
|
51
51
|
};
|
package/src/index.ts
CHANGED
|
@@ -53,6 +53,8 @@ export { createTranslate } from './i18n/createTranslate';
|
|
|
53
53
|
// io
|
|
54
54
|
export { isBuffer } from './io/isBuffer';
|
|
55
55
|
export { isTransferable } from './io/isTransferable';
|
|
56
|
+
export { ArrayBuffers } from './io/ArrayBuffers';
|
|
57
|
+
export type { AbstractEncoding } from './io/AbstractEncoding';
|
|
56
58
|
|
|
57
59
|
// browser
|
|
58
60
|
export { copy } from './browsers/copy';
|
|
@@ -67,8 +69,7 @@ export { structuredClone } from './isomorphics/structuredClone';
|
|
|
67
69
|
// crypto
|
|
68
70
|
export { randomUUID } from './crypto/randomUUID';
|
|
69
71
|
export { sha1, sha256, sha384, sha512 } from './crypto/hashing';
|
|
70
|
-
export { hex } from './crypto/
|
|
72
|
+
export { hex } from './crypto/base';
|
|
71
73
|
|
|
72
74
|
// misc
|
|
73
75
|
export { createRandom } from './maths/random';
|
|
74
|
-
export { urljoin } from './shim/urljoin';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AbstractEncoding contract
|
|
3
|
+
*
|
|
4
|
+
* @see https://github.com/mafintosh/abstract-encoding
|
|
5
|
+
*/
|
|
6
|
+
export interface AbstractEncoding<T> {
|
|
7
|
+
/**
|
|
8
|
+
* encode a value to a buffer
|
|
9
|
+
*/
|
|
10
|
+
encode(data: T, buffer?: ArrayBuffer, offset?: number): BufferSource;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* decode data from buffer
|
|
14
|
+
*/
|
|
15
|
+
decode(buffer: BufferSource, start?: number, end?: number): T;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* byteLength of data
|
|
19
|
+
*/
|
|
20
|
+
byteLength?: (data: T) => number;
|
|
21
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import test from 'ava';
|
|
2
|
+
import { ArrayBuffers } from './ArrayBuffers';
|
|
3
|
+
|
|
4
|
+
test.before(() => {
|
|
5
|
+
ArrayBuffers._allowedBuffer = false;
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
test('base64: ignore whitespace', function (t) {
|
|
9
|
+
const text = '\n YW9ldQ== ';
|
|
10
|
+
const buf = ArrayBuffers.from(text, 'base64');
|
|
11
|
+
t.is(ArrayBuffers.toString(buf), 'aoeu');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('base64: strings without padding', function (t) {
|
|
15
|
+
t.is(ArrayBuffers.toString(ArrayBuffers.from('YW9ldQ', 'base64')), 'aoeu');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('base64: newline in utf8 -- should not be an issue', function (t) {
|
|
19
|
+
t.is(
|
|
20
|
+
ArrayBuffers.toString(
|
|
21
|
+
ArrayBuffers.from('LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK', 'base64'),
|
|
22
|
+
'utf8',
|
|
23
|
+
),
|
|
24
|
+
'---\ntitle: Three dashes marks the spot\ntags:\n',
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('base64: newline in base64 -- should get stripped', function (t) {
|
|
29
|
+
t.is(
|
|
30
|
+
ArrayBuffers.toString(
|
|
31
|
+
ArrayBuffers.from(
|
|
32
|
+
'LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\nICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt',
|
|
33
|
+
'base64',
|
|
34
|
+
),
|
|
35
|
+
'utf8',
|
|
36
|
+
),
|
|
37
|
+
'---\ntitle: Three dashes marks the spot\ntags:\n - yaml\n - front-matter\n - dashes\nexpaned-',
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('base64: tab characters in base64 - should get stripped', function (t) {
|
|
42
|
+
t.is(
|
|
43
|
+
ArrayBuffers.toString(
|
|
44
|
+
ArrayBuffers.from(
|
|
45
|
+
'LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\t\t\t\tICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt',
|
|
46
|
+
'base64',
|
|
47
|
+
),
|
|
48
|
+
'utf8',
|
|
49
|
+
),
|
|
50
|
+
'---\ntitle: Three dashes marks the spot\ntags:\n - yaml\n - front-matter\n - dashes\nexpaned-',
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('base64: invalid non-alphanumeric characters -- should be stripped', function (t) {
|
|
55
|
+
t.is(ArrayBuffers.toString(ArrayBuffers.from('!"#$%&\'()*,.:;<=>?@[\\]^`{|}~', 'base64'), 'utf8'), '');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('base64: high byte', function (t) {
|
|
59
|
+
const highByte = ArrayBuffers.from([128]);
|
|
60
|
+
t.deepEqual(ArrayBuffers.alloc(1, ArrayBuffers.toString(highByte, 'base64'), 'base64'), highByte);
|
|
61
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import test from 'ava';
|
|
2
|
+
import { ArrayBuffers } from './ArrayBuffers';
|
|
3
|
+
|
|
4
|
+
test('concat', (t) => {
|
|
5
|
+
const check = (Type: any = Uint8Array, va = [1, 2, 3], vb = [4, 5, 6]) => {
|
|
6
|
+
const a = new Type(va);
|
|
7
|
+
const b = new Type(vb);
|
|
8
|
+
t.deepEqual(ArrayBuffers.concat([a, b]), new Type([...va, ...vb]).buffer, `should concat ${Type}`);
|
|
9
|
+
};
|
|
10
|
+
check(Uint8Array);
|
|
11
|
+
check(Uint16Array, [0xff, 0xffff]);
|
|
12
|
+
check(Uint32Array, [0xfffffff]);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('asView for Buffer', (t) => {
|
|
16
|
+
const buf = Buffer.from([1, 2, 3, 4, 5]);
|
|
17
|
+
t.is(ArrayBuffers.asView(Buffer, buf), buf);
|
|
18
|
+
const uint8 = ArrayBuffers.asView(Uint8Array, buf);
|
|
19
|
+
t.is(ArrayBuffers.asView(Buffer, uint8).buffer, buf.buffer);
|
|
20
|
+
|
|
21
|
+
t.deepEqual(uint8, buf);
|
|
22
|
+
t.deepEqual(ArrayBuffers.asView(Buffer, uint8), buf);
|
|
23
|
+
});
|