Skip to content

Commit

Permalink
feat: Handle file:// syntax in yaml parser
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Mar 24, 2018
1 parent 550ccac commit f21dcbc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/yaml-format/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ const handle_urls = (component) => {
return component
}

const handle_files = (component) => {
if (typeof component.data === 'string' && component.data.match(/file:/))
return {
'component': component.component,
'args': Object.assign({
'loader': component.data.match(/file.*\.(.*)$/)[1],
'is_file': true
}, component.args),
'data': component.data
}

return component
}

const handle_attr_syntax = (component) => {
if (component.data.map === undefined) return component
const attrs = component.data.filter((x) => Object.keys(x)[0].match(/attr:.*/))
Expand Down Expand Up @@ -112,8 +126,8 @@ const parser = (input) => {
for (const rule of rules) {
const [ patterns, func ] = rule
for (const pattern of patterns) {
if (key.match(pattern)) return handle_urls(
handle_attr_syntax(func(key.match(pattern), value)))
if (key.match(pattern)) return handle_files(handle_urls(
handle_attr_syntax(func(key.match(pattern), value))))
}
}

Expand Down
51 changes: 51 additions & 0 deletions src/yaml-format/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,57 @@ 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 parser = injector({
'js-yaml': {'safeLoad': safeLoadSpy},
}).default

return { parser }
}

const tests = [
{
'input': {'h1 text': 'file://example.com/text.csv'},
'output': {
'component': 'text',
'args': {'loader': 'csv', 'tagName': 'h1', 'is_file': true},
'data': 'file://example.com/text.csv'
}
},
{
'input': {'h1 text': 'file://example.com/text.json'},
'output': {
'component': 'text',
'args': {'loader': 'json', 'tagName': 'h1', 'is_file': true},
'data': 'file://example.com/text.json'
}
},
{
'input': {'h1 text': [
{'attr:loader': 'csv'},
{'data': 'file://example.com/text.json'}
]},
'output': {
'component': 'text',
'args': {'loader': 'csv', 'tagName': 'h1', 'is_file': true},
'data': 'file://example.com/text.json'
}
}
]


tests.forEach(function({input, output}) {
it(`${Object.keys(input)[0]} - ${input[Object.keys(input)[0]]}`, () => {
const { parser } = set_up(input)
assert.deepEqual(parser(input), output)
})
})
})


describe('yaml format - handling URL', function() {
const set_up = function() {
const injector = require('inject-loader!./parser.js')
Expand Down

0 comments on commit f21dcbc

Please sign in to comment.