GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — algorithm/stringify/array.test.js at master · selfstudygo/algorithm

Expand file tree

/

array.test.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
139 lines (123 loc) · 4.49 KB
/

array.test.js

Copy path

File metadata and controls

139 lines (123 loc) · 4.49 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// const a = [1, "ab\"c", true, undefined, null, _=>3, Symbol()];
// JSON.stringify(a) === arrayStringify(a);가 되도록 arrayStringify를 구현
// [1,"ab\"c",true,null,null,null,null]
const validator = {
isArray(arr) {
return Array.isArray(arr);
}
};
const {arrayForPublic, arrayRecursivePublic, arrayRecursiveTailPublic} = (() => {
const trimAndWrap = (str, start, end) => {
return `${start}${str.replace(/,\s*$/, '')}${end}`;
};
const separator = () => ',';
const processChar = (char) => {
switch (char) {
case '\\':
return '\\\\';
case '\n':
return '\\n';
case '\t':
return '\\t';
case '\v':
return '\\v';
case '\b':
return '\\b';
case '\r':
return '\\r';
case '\s':
return 's';
case '"' :
return '\\"';
default:
return char;
}
};
const quote = (str) => {
return `"${str}"`;
};
const stringifyCore = (v, strHandler) => {
const t = typeof v;
switch (t) {
case 'boolean':
return v.toString();
case 'number': {
if (v === 0) {
// -0
return '0';
}
if (isNaN(v)) {
return null;
}
if (!isFinite(v)) {
return null;
}
return v.toString();
}
case 'string':
return quote(strHandler(v, 0, ''));
default:
// unknown
return null;
}
};
// for recursiveTail
const arrayRecursive = (arr, i, processString) => {
if (arr.length === i) return '';
return stringifyCore(arr[i], processString) + separator() + arrayRecursive(arr, i + 1, processString);
};
const arrayRecursivePublic = (arr) => {
if (!validator.isArray(arr)) throw `non-supporting type: ${typeof arr}`;
const processString = (str, i) => {
return str.length === i ? '' : (processChar(str[i]) + processString(str, i + 1));
}
return trimAndWrap(arrayRecursive(arr, 0, processString), '[', ']');
};
// for recursiveTail
const arrayRecursiveTail = (arr, i, str, processString) => {
if (arr.length === i) return str;
return arrayRecursiveTail(arr, i + 1, str + stringifyCore(arr[i], processString) + separator(), processString);
};
const arrayRecursiveTailPublic = (arr) => {
if (!validator.isArray(arr)) throw `non-supporting type: ${typeof arr}`;
const processString = (str, i, res) => {
return str.length === i ? res : processString(str, i + 1, res + processChar(str[i]));
}
return trimAndWrap(arrayRecursiveTail(arr, 0, '', processString), '[', ']');
};
// for loop
const arrayFor = (arr, processString) => {
let str = '';
let len = arr.length;
for (let i = 0; i < len; i++) {
str += stringifyCore(arr[i], processString) + separator()
}
return str;
};
const arrayForPublic = (arr) => {
if (!validator.isArray(arr)) throw `non-supporting type: ${typeof arr}`;
const processString = (str) => {
let newStr = '';
for (let i = 0; i < str.length; i++) {
newStr += processChar(str[i]);
}
return newStr;
}
return trimAndWrap(arrayFor(arr, processString), '[', ']');
};
return {arrayForPublic, arrayRecursivePublic, arrayRecursiveTailPublic};
})();
describe('stringify', () => {
it('[1, "ab\\"c", true, undefined, null, _=>3, Symbol()]', () => {
const a = [1, "ab\"c", true, 'a\bc', null, _ => 3, Symbol(), 'a\nb', '`${a}`', '\s', '\\s', '\\'];
expect(arrayRecursivePublic(a)).toBe(JSON.stringify(a));
})
it('[1, "ab\\"c", true, undefined, null, _=>3, Symbol()]', () => {
const a = [1, "ab\"c", true, 'a\bc', null, _ => 3, Symbol(), 'a\nb', '`${a}`', '\s', '\\s', '\\'];
expect(arrayRecursiveTailPublic(a)).toBe(JSON.stringify(a));
})
it('[1, "ab\\"c", true, undefined, null, _=>3, Symbol()]', () => {
const a = [1, "ab\"c", true, 'a\bc', null, _ => 3, Symbol(), 'a\nb', '`${a}`', '\s', '\\s', '\\'];
expect(arrayForPublic(a)).toBe(JSON.stringify(a));
})
})
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.