Skip to content

Commit 550e763

Browse files
committed
snake_case
1 parent 093543c commit 550e763

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

devalue.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';
2-
const unsafeChars = /[<>\b\f\n\r\t\0\u2028\u2029]/g;
2+
const unsafe_chars = /[<>\b\f\n\r\t\0\u2028\u2029]/g;
33
const reserved =
44
/^(?:do|if|in|for|int|let|new|try|var|byte|case|char|else|enum|goto|long|this|void|with|await|break|catch|class|const|final|float|short|super|throw|while|yield|delete|double|export|import|native|return|switch|throws|typeof|boolean|default|extends|finally|package|private|abstract|continue|debugger|function|volatile|interface|protected|transient|implements|instanceof|synchronized)$/;
55

@@ -18,7 +18,7 @@ const escaped = {
1818
'\u2028': '\\u2028',
1919
'\u2029': '\\u2029'
2020
};
21-
const objectProtoOwnPropertyNames = Object.getOwnPropertyNames(Object.prototype)
21+
const object_proto_names = Object.getOwnPropertyNames(Object.prototype)
2222
.sort()
2323
.join('\0');
2424

@@ -42,8 +42,8 @@ export function devalue(value) {
4242

4343
counts.set(thing, 1);
4444

45-
if (!isPrimitive(thing)) {
46-
const type = getType(thing);
45+
if (!is_primitive(thing)) {
46+
const type = get_type(thing);
4747

4848
switch (type) {
4949
case 'Number':
@@ -70,7 +70,7 @@ export function devalue(value) {
7070
proto !== Object.prototype &&
7171
proto !== null &&
7272
Object.getOwnPropertyNames(proto).sort().join('\0') !==
73-
objectProtoOwnPropertyNames
73+
object_proto_names
7474
) {
7575
throw new Error(`Cannot stringify arbitrary non-POJOs`);
7676
}
@@ -92,7 +92,7 @@ export function devalue(value) {
9292
.filter((entry) => entry[1] > 1)
9393
.sort((a, b) => b[1] - a[1])
9494
.forEach((entry, i) => {
95-
names.set(entry[0], getName(i));
95+
names.set(entry[0], get_name(i));
9696
});
9797

9898
/**
@@ -104,11 +104,11 @@ export function devalue(value) {
104104
return names.get(thing);
105105
}
106106

107-
if (isPrimitive(thing)) {
108-
return stringifyPrimitive(thing);
107+
if (is_primitive(thing)) {
108+
return stringify_primitive(thing);
109109
}
110110

111-
const type = getType(thing);
111+
const type = get_type(thing);
112112

113113
switch (type) {
114114
case 'Number':
@@ -117,7 +117,9 @@ export function devalue(value) {
117117
return `Object(${stringify(thing.valueOf())})`;
118118

119119
case 'RegExp':
120-
return `new RegExp(${stringifyString(thing.source)}, "${thing.flags}")`;
120+
return `new RegExp(${stringify_string(thing.source)}, "${
121+
thing.flags
122+
}")`;
121123

122124
case 'Date':
123125
return `new Date(${thing.getTime()})`;
@@ -135,7 +137,7 @@ export function devalue(value) {
135137

136138
default:
137139
const obj = `{${Object.keys(thing)
138-
.map((key) => `${safeKey(key)}:${stringify(thing[key])}`)
140+
.map((key) => `${safe_key(key)}:${stringify(thing[key])}`)
139141
.join(',')}}`;
140142
const proto = Object.getPrototypeOf(thing);
141143
if (proto === null) {
@@ -163,12 +165,12 @@ export function devalue(value) {
163165
names.forEach((name, thing) => {
164166
params.push(name);
165167

166-
if (isPrimitive(thing)) {
167-
values.push(stringifyPrimitive(thing));
168+
if (is_primitive(thing)) {
169+
values.push(stringify_primitive(thing));
168170
return;
169171
}
170172

171-
const type = getType(thing);
173+
const type = get_type(thing);
172174

173175
switch (type) {
174176
case 'Number':
@@ -215,7 +217,9 @@ export function devalue(value) {
215217
Object.getPrototypeOf(thing) === null ? 'Object.create(null)' : '{}'
216218
);
217219
Object.keys(thing).forEach((key) => {
218-
statements.push(`${name}${safeProp(key)}=${stringify(thing[key])}`);
220+
statements.push(
221+
`${name}${safe_prop(key)}=${stringify(thing[key])}`
222+
);
219223
});
220224
}
221225
});
@@ -231,7 +235,7 @@ export function devalue(value) {
231235
}
232236

233237
/** @param {number} num */
234-
function getName(num) {
238+
function get_name(num) {
235239
let name = '';
236240

237241
do {
@@ -243,13 +247,13 @@ function getName(num) {
243247
}
244248

245249
/** @param {any} thing */
246-
function isPrimitive(thing) {
250+
function is_primitive(thing) {
247251
return Object(thing) !== thing;
248252
}
249253

250254
/** @param {any} thing */
251-
function stringifyPrimitive(thing) {
252-
if (typeof thing === 'string') return stringifyString(thing);
255+
function stringify_primitive(thing) {
256+
if (typeof thing === 'string') return stringify_string(thing);
253257
if (thing === void 0) return 'void 0';
254258
if (thing === 0 && 1 / thing < 0) return '-0';
255259
const str = String(thing);
@@ -259,36 +263,36 @@ function stringifyPrimitive(thing) {
259263
}
260264

261265
/** @param {any} thing */
262-
function getType(thing) {
266+
function get_type(thing) {
263267
return Object.prototype.toString.call(thing).slice(8, -1);
264268
}
265269

266270
/** @param {string} c */
267-
function escapeUnsafeChar(c) {
271+
function escape_unsafe_char(c) {
268272
return escaped[c] || c;
269273
}
270274

271275
/** @param {string} str */
272-
function escapeUnsafeChars(str) {
273-
return str.replace(unsafeChars, escapeUnsafeChar);
276+
function escape_unsafe_chars(str) {
277+
return str.replace(unsafe_chars, escape_unsafe_char);
274278
}
275279

276280
/** @param {string} key */
277-
function safeKey(key) {
281+
function safe_key(key) {
278282
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key)
279283
? key
280-
: escapeUnsafeChars(JSON.stringify(key));
284+
: escape_unsafe_chars(JSON.stringify(key));
281285
}
282286

283287
/** @param {string} key */
284-
function safeProp(key) {
288+
function safe_prop(key) {
285289
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key)
286290
? `.${key}`
287-
: `[${escapeUnsafeChars(JSON.stringify(key))}]`;
291+
: `[${escape_unsafe_chars(JSON.stringify(key))}]`;
288292
}
289293

290294
/** @param {string} str */
291-
function stringifyString(str) {
295+
function stringify_string(str) {
292296
let result = '"';
293297

294298
for (let i = 0; i < str.length; i += 1) {

0 commit comments

Comments
 (0)