Skip to content

Commit 017c337

Browse files
committed
perf: Replace js-yaml with yamljs
1 parent 4bb218c commit 017c337

File tree

4 files changed

+49
-55
lines changed

4 files changed

+49
-55
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"jq-web": "^0.1.3",
3232
"lodash.isequal": "^4.5.0",
3333
"sassline": "^2.1.2",
34-
"spinkit": "^1.2.5"
34+
"spinkit": "^1.2.5",
35+
"yamljs": "^0.3.0"
3536
},
3637
"devDependencies": {
3738
"babel-cli": "^6.26.0",
@@ -57,7 +58,6 @@
5758
"hard-source-webpack-plugin": "^0.7.0-alpha.0",
5859
"html-webpack-plugin": "^3.2.0",
5960
"inject-loader": "^4.0.1",
60-
"js-yaml": "^3.10.0",
6161
"jsdoc": "^3.5.5",
6262
"jsdoc-babel": "^0.4.0",
6363
"jsdom": "^11.10.0",
@@ -83,7 +83,6 @@
8383
"webpack-cli": "^2.1.3",
8484
"webpack-dev-server": "^3.1.4",
8585
"webpack-node-externals": "^1.7.2",
86-
"webpack-why": "^0.2.2",
8786
"webshot": "^0.18.0"
8887
},
8988
"nyc": {

src/yaml-format/parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import yaml from 'js-yaml'
1+
import YAML from 'yamljs'
22

33
const rules = [
44
[[/dashboard ["]([^"]*)["]/, /dashboard [']([^']*)[']/], (match, value) => ({
@@ -120,7 +120,7 @@ export const error_message = message => ({
120120
const parser = (input) => {
121121
try {
122122
const yaml_contents = (typeof input === 'string')
123-
? yaml.safeLoad(input) : input
123+
? YAML.parse(input) : input
124124
if (yaml_contents === undefined)
125125
return error_message('A non-empty input file is required')
126126

src/yaml-format/parser.test.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,66 @@ import sinon from 'sinon'
55

66
describe('yaml format - parser', function() {
77

8-
const set_up = function({safeLoadSpyReturns, safeLoadSpyThrows}) {
8+
const set_up = function({parseSpyReturns, parseSpyThrows}) {
99
const injector = require('inject-loader!./parser.js')
10-
let safeLoadSpy = sinon.stub().returns(safeLoadSpyReturns)
11-
if (safeLoadSpyThrows)
12-
safeLoadSpy = safeLoadSpy.throws(new Error(safeLoadSpyThrows))
10+
let parseSpy = sinon.stub().returns(parseSpyReturns)
11+
if (parseSpyThrows)
12+
parseSpy = parseSpy.throws(new Error(parseSpyThrows))
1313
const injected = injector({
14-
'js-yaml': {'safeLoad': safeLoadSpy},
14+
'yamljs': {parse: parseSpy},
1515
})
1616

1717
const parser = injected.default
1818
const error_message = injected.error_message
1919

20-
return { parser, safeLoadSpy, error_message }
20+
return { parser, parseSpy, error_message }
2121
}
2222

2323
it('should display error for empty input', function() {
24-
const { parser, error_message } = set_up({'safeLoadSpyReturns': undefined})
24+
const { parser, error_message } = set_up({'parseSpyReturns': undefined})
2525
parser('').should.deepEqual(
2626
error_message('A non-empty input file is required'))
2727
})
2828

2929
it('should display error thrown by safeLoad', function() {
3030
const error = 'foo bar baz!'
31-
const { parser, error_message } = set_up({'safeLoadSpyThrows': error})
31+
const { parser, error_message } = set_up({'parseSpyThrows': error})
3232
parser('').should.deepEqual(
3333
error_message('Error: ' + error))
3434
})
3535

3636
it('should not display error for valid input', function() {
37-
const { parser, error_message } = set_up({'safeLoadSpyReturns': {
37+
const { parser, error_message } = set_up({'parseSpyReturns': {
3838
'dashboard "Hello World"': []
3939
}})
4040
parser('dashboard "Hello World": []').should.not.deepEqual(
4141
error_message('A non-empty input file is required'))
4242
})
4343

4444
it('yaml is only parsed if input is a string', function() {
45-
const { parser, safeLoadSpy } = set_up({'safeLoadSpyReturns': []})
45+
const { parser, parseSpy } = set_up({'parseSpyReturns': []})
4646
parser({'h1 text': ''})
47-
safeLoadSpy.should.not.be.called()
47+
parseSpy.should.not.be.called()
4848
})
4949

5050
const inputs = ['foo', 'bar']
5151

5252
inputs.forEach((arg) =>
5353
it(`yaml called with input - ${arg}`, function() {
54-
const { parser, safeLoadSpy } = set_up(
55-
{'safeLoadSpyReturns': {'dashboard "a"': []}})
54+
const { parser, parseSpy } = set_up(
55+
{'parseSpyReturns': {'dashboard "a"': []}})
5656
parser(arg)
57-
safeLoadSpy.should.be.calledWith(arg)
57+
parseSpy.should.be.calledWith(arg)
5858
})
5959
)
6060
})
6161

6262
describe('yaml format - root component', function() {
6363
const set_up = function() {
6464
const injector = require('inject-loader!./parser.js')
65-
const safeLoadSpy = sinon.spy(x => x)
65+
const parseSpy = sinon.spy(x => x)
6666
const parser = injector({
67-
'js-yaml': {'safeLoad': safeLoadSpy},
67+
'yamljs': {parse: parseSpy},
6868
}).default
6969

7070
return { parser }
@@ -147,11 +147,11 @@ describe('yaml format - root component', function() {
147147

148148

149149
describe('yaml format - text component', function() {
150-
const set_up = function(safeLoadSpyReturns) {
150+
const set_up = function(parseSpyReturns) {
151151
const injector = require('inject-loader!./parser.js')
152-
const safeLoadSpy = sinon.stub().returns(safeLoadSpyReturns)
152+
const parseSpy = sinon.stub().returns(parseSpyReturns)
153153
const parser = injector({
154-
'js-yaml': {'safeLoad': safeLoadSpy},
154+
'yamljs': {parse: parseSpy},
155155
}).default
156156

157157
return { parser }
@@ -197,9 +197,9 @@ describe('yaml format - text component', function() {
197197
describe('yaml format - board component', function() {
198198
const set_up = function() {
199199
const injector = require('inject-loader!./parser.js')
200-
const safeLoadSpy = sinon.spy(x => x)
200+
const parseSpy = sinon.spy(x => x)
201201
const parser = injector({
202-
'js-yaml': {'safeLoad': safeLoadSpy},
202+
'yamljs': {parse: parseSpy},
203203
}).default
204204

205205
return { parser }
@@ -252,9 +252,9 @@ describe('yaml format - board component', function() {
252252
describe('yaml format - rows component', function() {
253253
const set_up = function() {
254254
const injector = require('inject-loader!./parser.js')
255-
const safeLoadSpy = sinon.spy(x => x)
255+
const parseSpy = sinon.spy(x => x)
256256
const parser = injector({
257-
'js-yaml': {'safeLoad': safeLoadSpy},
257+
'yamljs': {parse: parseSpy},
258258
}).default
259259

260260
return { parser }
@@ -304,9 +304,9 @@ describe('yaml format - rows component', function() {
304304
describe('yaml format - columns component', function() {
305305
const set_up = function() {
306306
const injector = require('inject-loader!./parser.js')
307-
const safeLoadSpy = sinon.spy(x => x)
307+
const parseSpy = sinon.spy(x => x)
308308
const parser = injector({
309-
'js-yaml': {'safeLoad': safeLoadSpy},
309+
'yamljs': {parse: parseSpy},
310310
}).default
311311

312312
return { parser }
@@ -383,9 +383,9 @@ describe('yaml format - columns component', function() {
383383
describe('yaml format - chart component', function() {
384384
const set_up = function() {
385385
const injector = require('inject-loader!./parser.js')
386-
const safeLoadSpy = sinon.spy(x => x)
386+
const parseSpy = sinon.spy(x => x)
387387
const parser = injector({
388-
'js-yaml': {'safeLoad': safeLoadSpy},
388+
'yamljs': {parse: parseSpy},
389389
}).default
390390

391391
return { parser }
@@ -486,9 +486,9 @@ describe('yaml format - chart component', function() {
486486
describe('yaml format - handling file', function() {
487487
const set_up = function() {
488488
const injector = require('inject-loader!./parser.js')
489-
const safeLoadSpy = sinon.spy(x => x)
489+
const parseSpy = sinon.spy(x => x)
490490
const parser = injector({
491-
'js-yaml': {'safeLoad': safeLoadSpy},
491+
'yamljs': {parse: parseSpy},
492492
}).default
493493

494494
return { parser }
@@ -537,9 +537,9 @@ describe('yaml format - handling file', function() {
537537
describe('yaml format - handling URL', function() {
538538
const set_up = function() {
539539
const injector = require('inject-loader!./parser.js')
540-
const safeLoadSpy = sinon.spy(x => x)
540+
const parseSpy = sinon.spy(x => x)
541541
const parser = injector({
542-
'js-yaml': {'safeLoad': safeLoadSpy},
542+
'yamljs': {parse: parseSpy},
543543
}).default
544544

545545
return { parser }
@@ -588,9 +588,9 @@ describe('yaml format - handling URL', function() {
588588
describe('yaml format - attr: syntax', function() {
589589
const set_up = function() {
590590
const injector = require('inject-loader!./parser.js')
591-
const safeLoadSpy = sinon.spy(x => x)
591+
const parseSpy = sinon.spy(x => x)
592592
const parser = injector({
593-
'js-yaml': {'safeLoad': safeLoadSpy},
593+
'yamljs': {parse: parseSpy},
594594
}).default
595595

596596
return { parser }
@@ -724,11 +724,11 @@ describe('integration tests', () => {
724724
})
725725

726726
describe('yaml format - dropdown component', function() {
727-
const set_up = function(safeLoadSpyReturns) {
727+
const set_up = function(parseSpyReturns) {
728728
const injector = require('inject-loader!./parser.js')
729-
const safeLoadSpy = sinon.stub().returns(safeLoadSpyReturns)
729+
const parseSpy = sinon.stub().returns(parseSpyReturns)
730730
const parser = injector({
731-
'js-yaml': {'safeLoad': safeLoadSpy},
731+
'yamljs': {parse: parseSpy},
732732
}).default
733733

734734
return { parser }

yarn.lock

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5180,7 +5180,7 @@ js-tokens@^3.0.0, js-tokens@^3.0.2:
51805180
version "3.0.2"
51815181
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
51825182

5183-
js-yaml@^3.10.0, js-yaml@^3.4.3, js-yaml@^3.9.0, js-yaml@^3.9.1:
5183+
js-yaml@^3.4.3, js-yaml@^3.9.0, js-yaml@^3.9.1:
51845184
version "3.11.0"
51855185
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.11.0.tgz#597c1a8bd57152f26d622ce4117851a51f5ebaef"
51865186
dependencies:
@@ -8867,10 +8867,6 @@ traverse@~0.6.6:
88678867
version "0.6.6"
88688868
resolved "https://registry.yarnpkg.com/traverse/-/traverse-0.6.6.tgz#cbdf560fd7b9af632502fed40f918c157ea97137"
88698869

8870-
treeify@^1.0.1:
8871-
version "1.1.0"
8872-
resolved "https://registry.yarnpkg.com/treeify/-/treeify-1.1.0.tgz#4e31c6a463accd0943879f30667c4fdaff411bb8"
8873-
88748870
trim-newlines@^1.0.0:
88758871
version "1.0.0"
88768872
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613"
@@ -9373,14 +9369,6 @@ webpack-sources@^1.0.1, webpack-sources@^1.1.0:
93739369
source-list-map "^2.0.0"
93749370
source-map "~0.6.1"
93759371

9376-
webpack-why@^0.2.2:
9377-
version "0.2.2"
9378-
resolved "https://registry.yarnpkg.com/webpack-why/-/webpack-why-0.2.2.tgz#79f807cc7a64a6072afeee792128ddc6ed522744"
9379-
dependencies:
9380-
chalk "^2.3.0"
9381-
treeify "^1.0.1"
9382-
yargs "^10.1.1"
9383-
93849372
webpack@^4.8.3:
93859373
version "4.8.3"
93869374
resolved "https://registry.yarnpkg.com/webpack/-/webpack-4.8.3.tgz#957c8e80000f9e5cc03d775e78b472d8954f4eeb"
@@ -9595,6 +9583,13 @@ yallist@^3.0.0, yallist@^3.0.2:
95959583
version "3.0.2"
95969584
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.2.tgz#8452b4bb7e83c7c188d8041c1a837c773d6d8bb9"
95979585

9586+
yamljs@^0.3.0:
9587+
version "0.3.0"
9588+
resolved "https://registry.yarnpkg.com/yamljs/-/yamljs-0.3.0.tgz#dc060bf267447b39f7304e9b2bfbe8b5a7ddb03b"
9589+
dependencies:
9590+
argparse "^1.0.7"
9591+
glob "^7.0.5"
9592+
95989593
yargs-parser@^5.0.0:
95999594
version "5.0.0"
96009595
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-5.0.0.tgz#275ecf0d7ffe05c77e64e7c86e4cd94bf0e1228a"
@@ -9647,7 +9642,7 @@ yargs@11.1.0, yargs@^11.1.0:
96479642
y18n "^3.2.1"
96489643
yargs-parser "^9.0.2"
96499644

9650-
yargs@^10.0.3, yargs@^10.1.1:
9645+
yargs@^10.0.3:
96519646
version "10.1.2"
96529647
resolved "https://registry.yarnpkg.com/yargs/-/yargs-10.1.2.tgz#454d074c2b16a51a43e2fb7807e4f9de69ccb5c5"
96539648
dependencies:

0 commit comments

Comments
 (0)