Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit 84ea22d

Browse files
committed
Refactor code
1 parent fa80e15 commit 84ea22d

File tree

5 files changed

+94
-95
lines changed

5 files changed

+94
-95
lines changed

src/array-stable.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { stringifyString } from './string';
2-
import { stringifyBuffer } from './buffer';
3-
import { stableStringifyObject } from './object-stable';
1+
import { serializeString } from './string';
2+
import { serializeBuffer } from './buffer';
3+
import { stableSerializeObject } from './object-stable';
44
import { seenObjects } from './seen-objects';
55

66
/**
7-
* stableStringifyArray() performs the same as stringifyArray() with the single exception that recursive calls to
8-
* stringifyObject() and stringifyArray() are replaced with calls to stableStringifyObject() and stableStringifyArray()
9-
* respectively.
7+
* Performs the same as stringifyArray() with the single exception that recursive calls to stringifyObject() and
8+
* stringifyArray() are replaced with calls to stableStringifyObject() and stableStringifyArray() respectively.
109
*/
1110

12-
export function stableStringifyArray(
11+
export function stableSerializeArray(
1312
arr: any[],
1413
compareFn: (a: [string, any], b: [string, any]) => number,
1514
safe: boolean,
@@ -22,8 +21,8 @@ export function stableStringifyArray(
2221
for (i = 0; i < arr.length; i++) {
2322
value = arr[i];
2423

25-
if (typeof value === 'boolean') {
26-
str += prefix + value;
24+
if (typeof value === 'string') {
25+
str += prefix + serializeString(value);
2726
prefix = ',';
2827
}
2928

@@ -32,14 +31,15 @@ export function stableStringifyArray(
3231
prefix = ',';
3332
}
3433

35-
else if (typeof value === 'string') {
36-
str += prefix + stringifyString(value);
34+
else if (typeof value === 'boolean') {
35+
str += prefix + value;
3736
prefix = ',';
3837
}
3938

40-
else if (value !== null && typeof value === 'object') {
39+
40+
else if (typeof value === 'object' && value !== null) {
4141
if (Buffer.isBuffer(value)) {
42-
str += prefix + stringifyBuffer(value);
42+
str += prefix + serializeBuffer(value);
4343
}
4444

4545
else if (value instanceof Date) {
@@ -56,8 +56,8 @@ export function stableStringifyArray(
5656

5757
str += prefix;
5858
str += Array.isArray(value)
59-
? stableStringifyArray(value, compareFn, true)
60-
: stableStringifyObject(value, compareFn, true);
59+
? stableSerializeArray(value, compareFn, true)
60+
: stableSerializeObject(value, compareFn, true);
6161
}
6262

6363
finally {
@@ -68,8 +68,8 @@ export function stableStringifyArray(
6868
else {
6969
str += prefix;
7070
str += Array.isArray(value)
71-
? stableStringifyArray(value, compareFn, false)
72-
: stableStringifyObject(value, compareFn, false);
71+
? stableSerializeArray(value, compareFn, false)
72+
: stableSerializeObject(value, compareFn, false);
7373
}
7474

7575
prefix = ',';

src/array.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { stringifyString } from './string';
2-
import { stringifyBuffer } from './buffer';
3-
import { stringifyObject } from './object';
1+
import { serializeString } from './string';
2+
import { serializeBuffer } from './buffer';
3+
import { serializeObject } from './object';
44
import { seenObjects } from './seen-objects';
55

66
/**
7-
* stringifyArray() iterates over all the elements of an array, converting them to strings one by one and then
8-
* concatenating them. The resulting string should match the output of JSON.stringify() with the exception that
9-
* toJSON() methods are ignored.
7+
* Iterates over all the elements of an array, converting them to strings one by one and then concatenating them. The
8+
* resulting string should match the output of JSON.stringify() with the exception that toJSON() methods are ignored.
109
*/
1110

12-
export function stringifyArray(arr: any[], safe: boolean): string {
11+
export function serializeArray(arr: any[], safe: boolean): string {
1312
let str = '[';
1413
let prefix = '';
1514
let i;
@@ -18,8 +17,8 @@ export function stringifyArray(arr: any[], safe: boolean): string {
1817
for (i = 0; i < arr.length; i++) {
1918
value = arr[i];
2019

21-
if (typeof value === 'boolean') {
22-
str += prefix + value;
20+
if (typeof value === 'string') {
21+
str += prefix + serializeString(value);
2322
prefix = ',';
2423
}
2524

@@ -28,14 +27,15 @@ export function stringifyArray(arr: any[], safe: boolean): string {
2827
prefix = ',';
2928
}
3029

31-
else if (typeof value === 'string') {
32-
str += prefix + stringifyString(value);
30+
31+
else if (typeof value === 'boolean') {
32+
str += prefix + value;
3333
prefix = ',';
3434
}
3535

36-
else if (value !== null && typeof value === 'object') {
36+
else if (typeof value === 'object' && value !== null) {
3737
if (Buffer.isBuffer(value)) {
38-
str += prefix + stringifyBuffer(value);
38+
str += prefix + serializeBuffer(value);
3939
}
4040

4141
else if (value instanceof Date) {
@@ -52,8 +52,8 @@ export function stringifyArray(arr: any[], safe: boolean): string {
5252

5353
str += prefix;
5454
str += Array.isArray(value)
55-
? stringifyArray(value, true)
56-
: stringifyObject(value, true);
55+
? serializeArray(value, true)
56+
: serializeObject(value, true);
5757
}
5858

5959
finally {
@@ -64,8 +64,8 @@ export function stringifyArray(arr: any[], safe: boolean): string {
6464
else {
6565
str += prefix;
6666
str += Array.isArray(value)
67-
? stringifyArray(value, false)
68-
: stringifyObject(value, false);
67+
? serializeArray(value, false)
68+
: serializeObject(value, false);
6969
}
7070

7171
prefix = ',';

src/index.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { stringifyString } from './string';
2-
import { stringifyBuffer } from './buffer';
3-
import { stringifyArray } from './array';
4-
import { stringifyObject } from './object';
5-
import { stableStringifyArray } from './array-stable';
6-
import { stableStringifyObject } from './object-stable';
1+
import { serializeString } from './string';
2+
import { serializeBuffer } from './buffer';
3+
import { serializeArray } from './array';
4+
import { serializeObject } from './object';
5+
import { stableSerializeArray } from './array-stable';
6+
import { stableSerializeObject } from './object-stable';
77

88
type CompareFunction = (a: [string, any], b: [string, any]) => number;
99

@@ -23,7 +23,7 @@ export function jsonStringify(value: any, safe = true): string | undefined {
2323
}
2424

2525
else if (typeof value === 'string') {
26-
return stringifyString(value);
26+
return serializeString(value);
2727
}
2828

2929
else if (value === null) {
@@ -32,7 +32,7 @@ export function jsonStringify(value: any, safe = true): string | undefined {
3232

3333
else if (typeof value === 'object') {
3434
if (Buffer.isBuffer(value)) {
35-
return stringifyBuffer(value);
35+
return serializeBuffer(value);
3636
}
3737

3838
else if (value instanceof Date) {
@@ -41,8 +41,8 @@ export function jsonStringify(value: any, safe = true): string | undefined {
4141

4242
else {
4343
return Array.isArray(value)
44-
? stringifyArray(value, safe)
45-
: stringifyObject(value, safe);
44+
? serializeArray(value, safe)
45+
: serializeObject(value, safe);
4646
}
4747
}
4848

@@ -69,7 +69,7 @@ export function stableJsonStringify(value: any, compareFn?: CompareFunction | nu
6969
}
7070

7171
else if (typeof value === 'string') {
72-
return stringifyString(value);
72+
return serializeString(value);
7373
}
7474

7575
else if (value === null) {
@@ -78,7 +78,7 @@ export function stableJsonStringify(value: any, compareFn?: CompareFunction | nu
7878

7979
else if (typeof value === 'object') {
8080
if (Buffer.isBuffer(value)) {
81-
return stringifyBuffer(value);
81+
return serializeBuffer(value);
8282
}
8383

8484
else if (value instanceof Date) {
@@ -89,8 +89,8 @@ export function stableJsonStringify(value: any, compareFn?: CompareFunction | nu
8989
compareFn = compareFn ?? defaultCompareKeys;
9090

9191
return Array.isArray(value)
92-
? stableStringifyArray(value, compareFn, safe)
93-
: stableStringifyObject(value, compareFn, safe);
92+
? stableSerializeArray(value, compareFn, safe)
93+
: stableSerializeObject(value, compareFn, safe);
9494
}
9595
}
9696

src/object-stable.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import { stringifyString } from './string';
2-
import { stringifyBuffer } from './buffer';
3-
import { stableStringifyArray } from './array-stable';
1+
import { serializeString } from './string';
2+
import { serializeBuffer } from './buffer';
3+
import { stableSerializeArray } from './array-stable';
44
import { seenObjects } from './seen-objects';
55

66
/**
7-
* stableStringifyObject() performs the same as stringifyObject(), except that object entries are sorted using the
8-
* provided compare function before being serialized. Also, recursive calls to stringifyObject() and stringifyArray()
9-
* are replaced with calls to stableStringifyObject() and stableStringifyArray() respectively.
7+
* Performs the same function as stringifyObject(), except that object entries are sorted using the
8+
* provided comparison function before being serialized.
109
*/
1110

12-
export function stableStringifyObject(
11+
export function stableSerializeObject(
1312
obj: any,
1413
compareFn: (a: [string, any], b: [string, any]) => number,
15-
safe: boolean
14+
safe: boolean,
1615
): string {
1716
const entries = Object.entries(obj).sort(compareFn);
1817

@@ -25,29 +24,29 @@ export function stableStringifyObject(
2524
for (i = 0; i < entries.length; i++) {
2625
[key, value] = entries[i];
2726

28-
if (typeof value === 'boolean') {
29-
str += prefix + key + '":' + value;
30-
prefix = ',"';
31-
}
32-
33-
else if (typeof value === 'number') {
34-
str += prefix + key + (value === Infinity || Number.isNaN(value) ? '":null' : '":' + value);
27+
if (typeof value === 'string') {
28+
str += prefix + key + '":' + serializeString(value);
3529
prefix = ',"';
3630
}
3731

38-
else if (typeof value === 'string') {
39-
str += prefix + key + '":' + stringifyString(value);
32+
else if (typeof value === 'boolean') {
33+
str += prefix + key + '":' + value;
4034
prefix = ',"';
4135
}
4236

43-
else if (value === null) {
44-
str += prefix + key + '":null';
37+
else if (typeof value === 'number') {
38+
str += prefix + key + (value === Infinity || Number.isNaN(value) ? '":null' : '":' + value);
4539
prefix = ',"';
4640
}
4741

4842
else if (typeof value === 'object') {
49-
if (Buffer.isBuffer(value)) {
50-
str += prefix + key + '":' + stringifyBuffer(value);
43+
if (value === null) {
44+
str += prefix + key + '":null';
45+
prefix = ',"';
46+
}
47+
48+
else if (Buffer.isBuffer(value)) {
49+
str += prefix + key + '":' + serializeBuffer(value);
5150
}
5251

5352
else if (value instanceof Date) {
@@ -64,8 +63,8 @@ export function stableStringifyObject(
6463

6564
str += prefix + key + '":';
6665
str += Array.isArray(value)
67-
? stableStringifyArray(value, compareFn, true)
68-
: stableStringifyObject(value, compareFn, true);
66+
? stableSerializeArray(value, compareFn, true)
67+
: stableSerializeObject(value, compareFn, true);
6968
}
7069

7170
finally {
@@ -76,8 +75,8 @@ export function stableStringifyObject(
7675
else {
7776
str += prefix + key + '":';
7877
str += Array.isArray(value)
79-
? stableStringifyArray(value, compareFn, false)
80-
: stableStringifyObject(value, compareFn, false);
78+
? stableSerializeArray(value, compareFn, false)
79+
: stableSerializeObject(value, compareFn, false);
8180
}
8281

8382
prefix = ',"';

src/object.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { stringifyString } from './string';
2-
import { stringifyBuffer } from './buffer';
3-
import { stringifyArray } from './array';
1+
import { serializeString } from './string';
2+
import { serializeBuffer } from './buffer';
3+
import { serializeArray } from './array';
44
import { seenObjects } from './seen-objects';
55

66
/**
7-
* stringifyObject() iterates over all the key/value pairs of an object, converting them to strings one by one and then
8-
* concatenating them. The resulting string should match the output of JSON.stringify() with the exception that
9-
* toJSON() methods are ignored.
7+
* Iterates over all the key/value pairs of an object, converting them to strings one by one and then concatenating
8+
* them. The resulting string should match the output of JSON.stringify(), with the exception that toJSON() methods are
9+
* ignored.
1010
*/
1111

12-
export function stringifyObject(obj: any, safe: boolean): string {
12+
export function serializeObject(obj: any, safe: boolean): string {
1313
let str = '{';
1414
let prefix = '"';
1515
let key;
@@ -18,8 +18,8 @@ export function stringifyObject(obj: any, safe: boolean): string {
1818
for (key in obj) {
1919
value = obj[key];
2020

21-
if (typeof value === 'boolean') {
22-
str += prefix + key + '":' + value;
21+
if (typeof value === 'string') {
22+
str += prefix + key + '":' + serializeString(value);
2323
prefix = ',"';
2424
}
2525

@@ -28,19 +28,19 @@ export function stringifyObject(obj: any, safe: boolean): string {
2828
prefix = ',"';
2929
}
3030

31-
else if (typeof value === 'string') {
32-
str += prefix + key + '":' + stringifyString(value);
33-
prefix = ',"';
34-
}
35-
36-
else if (value === null) {
37-
str += prefix + key + '":null';
31+
else if (typeof value === 'boolean') {
32+
str += prefix + key + '":' + value;
3833
prefix = ',"';
3934
}
4035

4136
else if (typeof value === 'object') {
42-
if (Buffer.isBuffer(value)) {
43-
str += prefix + key + '":' + stringifyBuffer(value);
37+
if (value === null) {
38+
str += prefix + key + '":null';
39+
prefix = ',"';
40+
}
41+
42+
else if (Buffer.isBuffer(value)) {
43+
str += prefix + key + '":' + serializeBuffer(value);
4444
}
4545

4646
else if (value instanceof Date) {
@@ -57,8 +57,8 @@ export function stringifyObject(obj: any, safe: boolean): string {
5757

5858
str += prefix + key + '":';
5959
str += Array.isArray(value)
60-
? stringifyArray(value, true)
61-
: stringifyObject(value, true);
60+
? serializeArray(value, true)
61+
: serializeObject(value, true);
6262
}
6363

6464
finally {
@@ -69,8 +69,8 @@ export function stringifyObject(obj: any, safe: boolean): string {
6969
else {
7070
str += prefix + key + '":';
7171
str += Array.isArray(value)
72-
? stringifyArray(value, false)
73-
: stringifyObject(value, false);
72+
? serializeArray(value, false)
73+
: serializeObject(value, false);
7474
}
7575

7676
prefix = ',"';

0 commit comments

Comments
 (0)