Skip to content

Commit

Permalink
replaceRevive [nfc]: Use function declarations.
Browse files Browse the repository at this point in the history
Greg pointed out (in response to d750645) [1] that the
`const foo = function foo() { ... }` form is repetitive, which it
is.

I'd given just a bit of reasoning for the repetitive form in
a0dcf4f, which I also gave in response to Greg's comment -- but
after more discussion, I was convinced that plain function
declarations (at toplevel, where definitions belong, and nontrivial
logic doesn't) are at least as good and possibly better. We'll fix
a0dcf4f in the next commit.

[1] zulip#4367 (comment)
  • Loading branch information
chrisbobbe authored and gnprice committed Jan 1, 2021
1 parent 08b77bb commit f9331fa
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/boot/replaceRevive.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const SERIALIZED_TYPE_FIELD_NAME_ESCAPED: '__serializedType__value' = '__seriali
// Don't make this an arrow function -- we need `this` to be a special
// value; see
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter.
const replacer = function replacer(key, value) {
function replacer(key, value) {
// The value at the current path before JSON.stringify called its
// `toJSON` method, if present.
//
Expand Down Expand Up @@ -109,7 +109,7 @@ const replacer = function replacer(key, value) {
}

return origValue;
};
}

/**
* Custom reviver for inventive data types JSON doesn't handle.
Expand All @@ -118,7 +118,7 @@ const replacer = function replacer(key, value) {
* reviving logic must also appear in `replacer` so they stay in
* sync.
*/
const reviver = function reviver(key, value) {
function reviver(key, value) {
if (value !== null && typeof value === 'object' && SERIALIZED_TYPE_FIELD_NAME in value) {
const data = value.data;
switch (value[SERIALIZED_TYPE_FIELD_NAME]) {
Expand All @@ -144,9 +144,9 @@ const reviver = function reviver(key, value) {
}
}
return value;
};
}

export const stringify = function stringify(data: mixed): string {
export function stringify(data: mixed): string {
const result = JSON.stringify(data, replacer);
if (result === undefined) {
// Flow says that the output for JSON.stringify could be
Expand All @@ -161,8 +161,8 @@ export const stringify = function stringify(data: mixed): string {
throw new Error('undefined result for stringify');
}
return result;
};
}

export const parse = function parse(data: string): mixed {
export function parse(data: string): mixed {
return JSON.parse(data, reviver);
};
}

0 comments on commit f9331fa

Please sign in to comment.