Skip to content

Commit baec4cb

Browse files
mrkishiRich-Harris
andauthored
Add prettier configuration (#140)
* fix: rename test file so uvu picks it up * chore: add simple benchmarks Based on Svelte's benchmark scaffolding. * feat: simplify TypedArray slices * feat: use native alternatives to encode/decode base64 * feat: whitelist `Float16Array` * fix: guarantee flatten return type * chore: add prettier configuration Add a `.prettierrc` file matching Svelte's. --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>
1 parent a210130 commit baec4cb

12 files changed

Lines changed: 87 additions & 115 deletions

File tree

.prettierrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"useTabs": true,
3+
"singleQuote": true,
4+
"trailingComma": "none",
5+
"printWidth": 100,
6+
"overrides": [
7+
{
8+
"files": ["**/CHANGELOG.md", "**/vite.config.js.timestamp-*", "**/test-results/**"],
9+
"options": {
10+
"rangeEnd": 0
11+
}
12+
}
13+
]
14+
}

benchmarking/benchmarks/typed-array.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default [
2626
stringify(value);
2727
}
2828
});
29-
},
29+
}
3030
},
3131

3232
{
@@ -44,7 +44,7 @@ export default [
4444
stringify(value);
4545
}
4646
});
47-
},
47+
}
4848
},
4949

5050
{
@@ -62,7 +62,7 @@ export default [
6262
stringify(value);
6363
}
6464
});
65-
},
65+
}
6666
},
6767

6868
{
@@ -80,7 +80,7 @@ export default [
8080
parse(string);
8181
}
8282
});
83-
},
83+
}
8484
},
8585

8686
{
@@ -98,7 +98,7 @@ export default [
9898
parse(string);
9999
}
100100
});
101-
},
101+
}
102102
},
103103

104104
{
@@ -116,6 +116,6 @@ export default [
116116
parse(string);
117117
}
118118
});
119-
},
120-
},
119+
}
120+
}
121121
];

benchmarking/compare/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const is_jj = execSync('git for-each-ref --count=1 refs/jj/').length > 0;
1010
const current_ref = exec(
1111
is_jj
1212
? 'jj show --no-patch --template change_id'
13-
: 'git symbolic-ref --short -q HEAD || git rev-parse --short HEAD',
13+
: 'git symbolic-ref --short -q HEAD || git rev-parse --short HEAD'
1414
);
1515

1616
/** @type {(branch: string) => void} */
@@ -95,9 +95,7 @@ for (let i = 0; i < results[0].length; i += 1) {
9595
const SIZE = 20;
9696
const n = Math.round(SIZE * (time / max));
9797

98-
console.log(
99-
`${char(b)}: ${'◼'.repeat(n)}${' '.repeat(SIZE - n)} ${time.toFixed(2)}ms`,
100-
);
98+
console.log(`${char(b)}: ${'◼'.repeat(n)}${' '.repeat(SIZE - n)} ${time.toFixed(2)}ms`);
10199
});
102100
console.groupEnd();
103101
}

benchmarking/run.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const filter_fn = filters.length ? (b) => filters.some((f) => b.label.includes(f
99
const suites = [
1010
{
1111
name: 'TypedArray benchmarks',
12-
benchmarks: typed_array_benchmarks.filter(filter_fn),
13-
},
12+
benchmarks: typed_array_benchmarks.filter(filter_fn)
13+
}
1414
].filter((suite) => suite.benchmarks.length > 0);
1515

1616
if (suites.length === 0) {
@@ -39,7 +39,7 @@ try {
3939
console.log(
4040
pad_right('Benchmark', COLUMN_WIDTHS[0]) +
4141
pad_left('Time', COLUMN_WIDTHS[1]) +
42-
pad_left('GC time', COLUMN_WIDTHS[2]),
42+
pad_left('GC time', COLUMN_WIDTHS[2])
4343
);
4444
console.log('='.repeat(TOTAL_WIDTH));
4545

@@ -48,7 +48,7 @@ try {
4848
console.log(
4949
pad_right(benchmark.label, COLUMN_WIDTHS[0]) +
5050
pad_left(results.time.toFixed(2), COLUMN_WIDTHS[1]) +
51-
pad_left(results.gc_time.toFixed(2), COLUMN_WIDTHS[2]),
51+
pad_left(results.gc_time.toFixed(2), COLUMN_WIDTHS[2])
5252
);
5353
total_time += results.time;
5454
total_gc_time += results.gc_time;
@@ -60,7 +60,7 @@ try {
6060
console.log(
6161
pad_right('suite', COLUMN_WIDTHS[0]) +
6262
pad_left(suite_time.toFixed(2), COLUMN_WIDTHS[1]) +
63-
pad_left(suite_gc_time.toFixed(2), COLUMN_WIDTHS[2]),
63+
pad_left(suite_gc_time.toFixed(2), COLUMN_WIDTHS[2])
6464
);
6565
console.log('='.repeat(TOTAL_WIDTH));
6666
}
@@ -74,5 +74,5 @@ console.log('');
7474
console.log(
7575
pad_right('total', COLUMN_WIDTHS[0]) +
7676
pad_left(total_time.toFixed(2), COLUMN_WIDTHS[1]) +
77-
pad_left(total_gc_time.toFixed(2), COLUMN_WIDTHS[2]),
77+
pad_left(total_gc_time.toFixed(2), COLUMN_WIDTHS[2])
7878
);

src/base64.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,7 @@ export function decode_legacy(base64) {
5454
}
5555

5656
const native = typeof Uint8Array.fromBase64 === 'function';
57-
const buffer =
58-
typeof process === 'object' && process.versions?.node !== undefined;
57+
const buffer = typeof process === 'object' && process.versions?.node !== undefined;
5958

60-
export const encode64 = native
61-
? encode_native
62-
: buffer
63-
? encode_buffer
64-
: encode_legacy;
65-
export const decode64 = native
66-
? decode_native
67-
: buffer
68-
? decode_buffer
69-
: decode_legacy;
59+
export const encode64 = native ? encode_native : buffer ? encode_buffer : encode_legacy;
60+
export const decode64 = native ? decode_native : buffer ? decode_buffer : decode_legacy;

src/parse.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ export function unflatten(parsed, revivers) {
6666
if (typeof value[0] === 'string') {
6767
const type = value[0];
6868

69-
const reviver =
70-
revivers && Object.hasOwn(revivers, type)
71-
? revivers[type]
72-
: undefined;
69+
const reviver = revivers && Object.hasOwn(revivers, type) ? revivers[type] : undefined;
7370

7471
if (reviver) {
7572
let i = value[1];
@@ -165,9 +162,10 @@ export function unflatten(parsed, revivers) {
165162
const TypedArrayConstructor = globalThis[type];
166163
const buffer = hydrate(value[1]);
167164

168-
hydrated[index] = value[2] !== undefined
169-
? new TypedArrayConstructor(buffer, value[2], value[3])
170-
: new TypedArrayConstructor(buffer);
165+
hydrated[index] =
166+
value[2] !== undefined
167+
? new TypedArrayConstructor(buffer, value[2], value[3])
168+
: new TypedArrayConstructor(buffer);
171169

172170
break;
173171
}

src/stringify.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function stringify(value, reducers) {
5252
if (thing === -Infinity) return NEGATIVE_INFINITY;
5353
if (thing === 0 && 1 / thing < 0) return NEGATIVE_ZERO;
5454

55-
if (indexes.has(thing)) return indexes.get(thing);
55+
if (indexes.has(thing)) return /** @type {number} */ (indexes.get(thing));
5656

5757
const index = p++;
5858
indexes.set(thing, index);
@@ -205,9 +205,7 @@ export function stringify(value, reducers) {
205205
str = '["Map"';
206206

207207
for (const [key, value] of thing) {
208-
keys.push(
209-
`.get(${is_primitive(key) ? stringify_primitive(key) : '...'})`
210-
);
208+
keys.push(`.get(${is_primitive(key) ? stringify_primitive(key) : '...'})`);
211209
str += `,${flatten(key)},${flatten(value)}`;
212210
keys.pop();
213211
}
@@ -263,21 +261,11 @@ export function stringify(value, reducers) {
263261

264262
default:
265263
if (!is_plain_object(thing)) {
266-
throw new DevalueError(
267-
`Cannot stringify arbitrary non-POJOs`,
268-
keys,
269-
thing,
270-
value
271-
);
264+
throw new DevalueError(`Cannot stringify arbitrary non-POJOs`, keys, thing, value);
272265
}
273266

274267
if (enumerable_symbols(thing).length > 0) {
275-
throw new DevalueError(
276-
`Cannot stringify POJOs with symbolic keys`,
277-
keys,
278-
thing,
279-
value
280-
);
268+
throw new DevalueError(`Cannot stringify POJOs with symbolic keys`, keys, thing, value);
281269
}
282270

283271
if (Object.getPrototypeOf(thing) === null) {

src/types.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Float16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
1+
export type TypedArray =
2+
| Int8Array
3+
| Uint8Array
4+
| Uint8ClampedArray
5+
| Int16Array
6+
| Uint16Array
7+
| Float16Array
8+
| Int32Array
9+
| Uint32Array
10+
| Float32Array
11+
| Float64Array
12+
| BigInt64Array
13+
| BigUint64Array;

src/uneval.js

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ export function uneval(value, replacer) {
7878

7979
case 'Map':
8080
for (const [key, value] of thing) {
81-
keys.push(
82-
`.get(${is_primitive(key) ? stringify_primitive(key) : '...'})`
83-
);
81+
keys.push(`.get(${is_primitive(key) ? stringify_primitive(key) : '...'})`);
8482
walk(value);
8583
keys.pop();
8684
}
@@ -116,21 +114,11 @@ export function uneval(value, replacer) {
116114

117115
default:
118116
if (!is_plain_object(thing)) {
119-
throw new DevalueError(
120-
`Cannot stringify arbitrary non-POJOs`,
121-
keys,
122-
thing,
123-
value
124-
);
117+
throw new DevalueError(`Cannot stringify arbitrary non-POJOs`, keys, thing, value);
125118
}
126119

127120
if (enumerable_symbols(thing).length > 0) {
128-
throw new DevalueError(
129-
`Cannot stringify POJOs with symbolic keys`,
130-
keys,
131-
thing,
132-
value
133-
);
121+
throw new DevalueError(`Cannot stringify POJOs with symbolic keys`, keys, thing, value);
134122
}
135123

136124
for (const key of Object.keys(thing)) {
@@ -188,9 +176,7 @@ export function uneval(value, replacer) {
188176
return `Object(${stringify(thing.valueOf())})`;
189177

190178
case 'RegExp':
191-
return `new RegExp(${stringify_string(thing.source)}, "${
192-
thing.flags
193-
}")`;
179+
return `new RegExp(${stringify_string(thing.source)}, "${thing.flags}")`;
194180

195181
case 'Date':
196182
return `new Date(${thing.getTime()})`;
@@ -260,12 +246,10 @@ export function uneval(value, replacer) {
260246
const d = String(thing.length).length;
261247

262248
const hole_cost = thing.length + 2;
263-
const sparse_cost = (25 + d) + population * (d + 2);
249+
const sparse_cost = 25 + d + population * (d + 2);
264250

265251
if (hole_cost > sparse_cost) {
266-
const entries = populated_keys
267-
.map((k) => `${k}:${stringify(thing[k])}`)
268-
.join(',');
252+
const entries = populated_keys.map((k) => `${k}:${stringify(thing[k])}`).join(',');
269253
return `Object.assign(Array(${thing.length}),{${entries}})`;
270254
}
271255

@@ -333,14 +317,10 @@ export function uneval(value, replacer) {
333317

334318
default:
335319
const keys = Object.keys(thing);
336-
const obj = keys
337-
.map((key) => `${safe_key(key)}:${stringify(thing[key])}`)
338-
.join(',');
320+
const obj = keys.map((key) => `${safe_key(key)}:${stringify(thing[key])}`).join(',');
339321
const proto = Object.getPrototypeOf(thing);
340322
if (proto === null) {
341-
return keys.length > 0
342-
? `{${obj},__proto__:null}`
343-
: `{__proto__:null}`;
323+
return keys.length > 0 ? `{${obj},__proto__:null}` : `{__proto__:null}`;
344324
}
345325

346326
return `{${obj}}`;
@@ -415,28 +395,20 @@ export function uneval(value, replacer) {
415395
break;
416396

417397
case 'ArrayBuffer':
418-
values.push(
419-
`new Uint8Array([${new Uint8Array(thing).join(',')}]).buffer`
420-
);
398+
values.push(`new Uint8Array([${new Uint8Array(thing).join(',')}]).buffer`);
421399
break;
422400

423401
default:
424-
values.push(
425-
Object.getPrototypeOf(thing) === null ? 'Object.create(null)' : '{}'
426-
);
402+
values.push(Object.getPrototypeOf(thing) === null ? 'Object.create(null)' : '{}');
427403
Object.keys(thing).forEach((key) => {
428-
statements.push(
429-
`${name}${safe_prop(key)}=${stringify(thing[key])}`
430-
);
404+
statements.push(`${name}${safe_prop(key)}=${stringify(thing[key])}`);
431405
});
432406
}
433407
});
434408

435409
statements.push(`return ${str}`);
436410

437-
return `(function(${params.join(',')}){${statements.join(
438-
';'
439-
)}}(${values.join(',')}))`;
411+
return `(function(${params.join(',')}){${statements.join(';')}}(${values.join(',')}))`;
440412
} else {
441413
return str;
442414
}
@@ -466,9 +438,7 @@ function escape_unsafe_chars(str) {
466438

467439
/** @param {string} key */
468440
function safe_key(key) {
469-
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key)
470-
? key
471-
: escape_unsafe_chars(JSON.stringify(key));
441+
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? key : escape_unsafe_chars(JSON.stringify(key));
472442
}
473443

474444
/** @param {string} key */

src/utils.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ export class DevalueError extends Error {
2929

3030
/** @param {any} thing */
3131
export function is_primitive(thing) {
32-
return Object(thing) !== thing;
32+
return thing === null || (typeof thing !== 'object' && typeof thing !== 'function');
3333
}
3434

35-
const object_proto_names = /* @__PURE__ */ Object.getOwnPropertyNames(
36-
Object.prototype
37-
)
35+
const object_proto_names = /* @__PURE__ */ Object.getOwnPropertyNames(Object.prototype)
3836
.sort()
3937
.join('\0');
4038

@@ -79,9 +77,7 @@ function get_escaped_char(char) {
7977
case '\u2029':
8078
return '\\u2029';
8179
default:
82-
return char < ' '
83-
? `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`
84-
: '';
80+
return char < ' ' ? `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}` : '';
8581
}
8682
}
8783

0 commit comments

Comments
 (0)