Skip to content

Commit

Permalink
perf: Replace js-yaml with yamljs
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed May 22, 2018
1 parent 4bb218c commit 017c337
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 55 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"jq-web": "^0.1.3",
"lodash.isequal": "^4.5.0",
"sassline": "^2.1.2",
"spinkit": "^1.2.5"
"spinkit": "^1.2.5",
"yamljs": "^0.3.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
Expand All @@ -57,7 +58,6 @@
"hard-source-webpack-plugin": "^0.7.0-alpha.0",
"html-webpack-plugin": "^3.2.0",
"inject-loader": "^4.0.1",
"js-yaml": "^3.10.0",
"jsdoc": "^3.5.5",
"jsdoc-babel": "^0.4.0",
"jsdom": "^11.10.0",
Expand All @@ -83,7 +83,6 @@
"webpack-cli": "^2.1.3",
"webpack-dev-server": "^3.1.4",
"webpack-node-externals": "^1.7.2",
"webpack-why": "^0.2.2",
"webshot": "^0.18.0"
},
"nyc": {
Expand Down
4 changes: 2 additions & 2 deletions src/yaml-format/parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import yaml from 'js-yaml'
import YAML from 'yamljs'

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

Expand Down
72 changes: 36 additions & 36 deletions src/yaml-format/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,66 @@ import sinon from 'sinon'

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

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

const parser = injected.default
const error_message = injected.error_message

return { parser, safeLoadSpy, error_message }
return { parser, parseSpy, error_message }
}

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

it('should display error thrown by safeLoad', function() {
const error = 'foo bar baz!'
const { parser, error_message } = set_up({'safeLoadSpyThrows': error})
const { parser, error_message } = set_up({'parseSpyThrows': error})
parser('').should.deepEqual(
error_message('Error: ' + error))
})

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

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

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

inputs.forEach((arg) =>
it(`yaml called with input - ${arg}`, function() {
const { parser, safeLoadSpy } = set_up(
{'safeLoadSpyReturns': {'dashboard "a"': []}})
const { parser, parseSpy } = set_up(
{'parseSpyReturns': {'dashboard "a"': []}})
parser(arg)
safeLoadSpy.should.be.calledWith(arg)
parseSpy.should.be.calledWith(arg)
})
)
})

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

return { parser }
Expand Down Expand Up @@ -147,11 +147,11 @@ describe('yaml format - root component', function() {


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

return { parser }
Expand Down Expand Up @@ -197,9 +197,9 @@ describe('yaml format - text component', function() {
describe('yaml format - board component', function() {
const set_up = function() {
const injector = require('inject-loader!./parser.js')
const safeLoadSpy = sinon.spy(x => x)
const parseSpy = sinon.spy(x => x)
const parser = injector({
'js-yaml': {'safeLoad': safeLoadSpy},
'yamljs': {parse: parseSpy},
}).default

return { parser }
Expand Down Expand Up @@ -252,9 +252,9 @@ describe('yaml format - board component', function() {
describe('yaml format - rows component', function() {
const set_up = function() {
const injector = require('inject-loader!./parser.js')
const safeLoadSpy = sinon.spy(x => x)
const parseSpy = sinon.spy(x => x)
const parser = injector({
'js-yaml': {'safeLoad': safeLoadSpy},
'yamljs': {parse: parseSpy},
}).default

return { parser }
Expand Down Expand Up @@ -304,9 +304,9 @@ describe('yaml format - rows component', function() {
describe('yaml format - columns component', function() {
const set_up = function() {
const injector = require('inject-loader!./parser.js')
const safeLoadSpy = sinon.spy(x => x)
const parseSpy = sinon.spy(x => x)
const parser = injector({
'js-yaml': {'safeLoad': safeLoadSpy},
'yamljs': {parse: parseSpy},
}).default

return { parser }
Expand Down Expand Up @@ -383,9 +383,9 @@ describe('yaml format - columns component', function() {
describe('yaml format - chart component', function() {
const set_up = function() {
const injector = require('inject-loader!./parser.js')
const safeLoadSpy = sinon.spy(x => x)
const parseSpy = sinon.spy(x => x)
const parser = injector({
'js-yaml': {'safeLoad': safeLoadSpy},
'yamljs': {parse: parseSpy},
}).default

return { parser }
Expand Down Expand Up @@ -486,9 +486,9 @@ describe('yaml format - chart component', function() {
describe('yaml format - handling file', function() {
const set_up = function() {
const injector = require('inject-loader!./parser.js')
const safeLoadSpy = sinon.spy(x => x)
const parseSpy = sinon.spy(x => x)
const parser = injector({
'js-yaml': {'safeLoad': safeLoadSpy},
'yamljs': {parse: parseSpy},
}).default

return { parser }
Expand Down Expand Up @@ -537,9 +537,9 @@ describe('yaml format - handling file', function() {
describe('yaml format - handling URL', function() {
const set_up = function() {
const injector = require('inject-loader!./parser.js')
const safeLoadSpy = sinon.spy(x => x)
const parseSpy = sinon.spy(x => x)
const parser = injector({
'js-yaml': {'safeLoad': safeLoadSpy},
'yamljs': {parse: parseSpy},
}).default

return { parser }
Expand Down Expand Up @@ -588,9 +588,9 @@ describe('yaml format - handling URL', function() {
describe('yaml format - attr: syntax', function() {
const set_up = function() {
const injector = require('inject-loader!./parser.js')
const safeLoadSpy = sinon.spy(x => x)
const parseSpy = sinon.spy(x => x)
const parser = injector({
'js-yaml': {'safeLoad': safeLoadSpy},
'yamljs': {parse: parseSpy},
}).default

return { parser }
Expand Down Expand Up @@ -724,11 +724,11 @@ describe('integration tests', () => {
})

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

return { parser }
Expand Down
23 changes: 9 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5180,7 +5180,7 @@ js-tokens@^3.0.0, js-tokens@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"

js-yaml@^3.10.0, js-yaml@^3.4.3, js-yaml@^3.9.0, js-yaml@^3.9.1:
js-yaml@^3.4.3, js-yaml@^3.9.0, js-yaml@^3.9.1:
version "3.11.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.11.0.tgz#597c1a8bd57152f26d622ce4117851a51f5ebaef"
dependencies:
Expand Down Expand Up @@ -8867,10 +8867,6 @@ traverse@~0.6.6:
version "0.6.6"
resolved "https://registry.yarnpkg.com/traverse/-/traverse-0.6.6.tgz#cbdf560fd7b9af632502fed40f918c157ea97137"

treeify@^1.0.1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/treeify/-/treeify-1.1.0.tgz#4e31c6a463accd0943879f30667c4fdaff411bb8"

trim-newlines@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613"
Expand Down Expand Up @@ -9373,14 +9369,6 @@ webpack-sources@^1.0.1, webpack-sources@^1.1.0:
source-list-map "^2.0.0"
source-map "~0.6.1"

webpack-why@^0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/webpack-why/-/webpack-why-0.2.2.tgz#79f807cc7a64a6072afeee792128ddc6ed522744"
dependencies:
chalk "^2.3.0"
treeify "^1.0.1"
yargs "^10.1.1"

webpack@^4.8.3:
version "4.8.3"
resolved "https://registry.yarnpkg.com/webpack/-/webpack-4.8.3.tgz#957c8e80000f9e5cc03d775e78b472d8954f4eeb"
Expand Down Expand Up @@ -9595,6 +9583,13 @@ yallist@^3.0.0, yallist@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.2.tgz#8452b4bb7e83c7c188d8041c1a837c773d6d8bb9"

yamljs@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/yamljs/-/yamljs-0.3.0.tgz#dc060bf267447b39f7304e9b2bfbe8b5a7ddb03b"
dependencies:
argparse "^1.0.7"
glob "^7.0.5"

yargs-parser@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-5.0.0.tgz#275ecf0d7ffe05c77e64e7c86e4cd94bf0e1228a"
Expand Down Expand Up @@ -9647,7 +9642,7 @@ [email protected], yargs@^11.1.0:
y18n "^3.2.1"
yargs-parser "^9.0.2"

yargs@^10.0.3, yargs@^10.1.1:
yargs@^10.0.3:
version "10.1.2"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-10.1.2.tgz#454d074c2b16a51a43e2fb7807e4f9de69ccb5c5"
dependencies:
Expand Down

0 comments on commit 017c337

Please sign in to comment.