Skip to content

Commit

Permalink
Fix bug close#11
Browse files Browse the repository at this point in the history
Make it enable to pick up data in several file and has same menu column
  • Loading branch information
KamataRyo authored and KamataRyo committed Feb 25, 2017
1 parent ee3ad87 commit 069f9ec
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 82 deletions.
35 changes: 17 additions & 18 deletions lib/wnew_csv2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,29 @@ module.exports = function(opts) {

menus.sort()

// push and send as a file stream
// push and send menu list as a file stream
const menuFile = origin.clone()
menuFile.contents = new Buffer(JSON.stringify(menus))
menuFile.path = `${menuFile.base}/${OPTS.menuHeader}.${OPTS.extension}`
this.push(menuFile)

// integrate item data
menus.map(menu => sources
// array of array of items from Vinyl
.map(sources => sources.items
// filter by menu for each menus
.filter(item => item.fileBaseName === menu)
.map(item => item.data)
)
// flatten
.reduce((a, b) => a.concat(b), [])
// push and send as a file stream for each items
.map(item => {
const itemFile = origin.clone()
itemFile.contents = new Buffer(JSON.stringify(item))
itemFile.path = `${itemFile.base}/${menu}.${OPTS.extension}`
this.push(itemFile)
// flatten items
const result = {}
sources.forEach(source => {
source.items.forEach(item => {
const key = item.fileBaseName
if (!result[key]) { result[key] = [] }
item.data.forEach(datum => result[key].push(datum))
})
)
})

// push and send item lists as file streams
Object.keys(result).forEach(key => {
const itemFile = origin.clone()
itemFile.contents = new Buffer(JSON.stringify(result[key]))
itemFile.path = `${itemFile.base}/${key}.${OPTS.extension}`
this.push(itemFile)
})

callback()
}
Expand Down
152 changes: 88 additions & 64 deletions tests/lib/wnew_csv2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,76 +289,100 @@ describe('gulp-csv2json', () => {

describe('several CSV files input', () => {

const CSV1 =
'menu,title,lat,lng,content\n' +
'串本,橋杭岩,12.345,123.45,これは橋杭岩です\n' +
'串本,潮岬,23.456,132.1,これは潮岬です'
const CSV2 =
'menu,title,lat,lng,content\n' +
'白浜,円月島,76.54,32.1,これは円月島です'

it('should return 3 files', done => {
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.length(3))
.pipe(assert.end(done))
})
describe('not splitted', () => {

it('should return menu array, firstly', done => {
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.first(file => isJSON(file, ['串本','白浜'])))
.pipe(assert.end(done))
})
const CSV1 =
'menu,title,lat,lng,content\n' +
'串本,橋杭岩,12.345,123.45,これは橋杭岩です\n' +
'串本,潮岬,23.456,132.1,これは潮岬です'
const CSV2 =
'menu,title,lat,lng,content\n' +
'白浜,円月島,76.54,32.1,これは円月島です'

it('should return converted json from csv, secondly', done => {
const item = [
{
'title': '橋杭岩',
'lat': '12.345',
'lng': '123.45',
'content': 'これは橋杭岩です'
},
{
'title': '潮岬',
'lat': '23.456',
'lng': '132.1',
'content': 'これは潮岬です'
}
]
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.second(file => isJSON(file, item)))
.pipe(assert.end(done))
})
it('should return 3 files', done => {
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.length(3))
.pipe(assert.end(done))
})

it('should be named propery', done => {
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.second(file => hasName(file, '串本.json')))
.pipe(assert.end(done))
})
it('should return menu array, firstly', done => {
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.first(file => isJSON(file, ['串本','白浜'])))
.pipe(assert.end(done))
})

it('should return converted json from csv, thirdly in this case', done => {
const item = [
{
'title': '円月島',
'lat': '76.54',
'lng': '32.1',
'content': 'これは円月島です'
}
]
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.nth(2, file => isJSON(file, item)))
.pipe(assert.end(done))
it('should return converted json from csv, secondly', done => {
const item = [
{
'title': '橋杭岩',
'lat': '12.345',
'lng': '123.45',
'content': 'これは橋杭岩です'
},
{
'title': '潮岬',
'lat': '23.456',
'lng': '132.1',
'content': 'これは潮岬です'
}
]
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.second(file => isJSON(file, item)))
.pipe(assert.end(done))
})

it('should be named propery', done => {
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.second(file => hasName(file, '串本.json')))
.pipe(assert.end(done))
})

it('should return converted json from csv, thirdly in this case', done => {
const item = [
{
'title': '円月島',
'lat': '76.54',
'lng': '32.1',
'content': 'これは円月島です'
}
]
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.nth(2, file => isJSON(file, item)))
.pipe(assert.end(done))
})

it('should be named propery', done => {
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.nth(2, file => hasName(file, '白浜.json')))
.pipe(assert.end(done))
})
})

it('should be named propery', done => {
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.nth(2, file => hasName(file, '白浜.json')))
.pipe(assert.end(done))
describe('splitted resource', () => {

const CSV1 =
'menu,title,lat,lng,content\n' +
'串本,橋杭岩,12.345,123.45,これは橋杭岩です\n' +
'串本,潮岬,23.456,132.1,これは潮岬です\n' +
'公衆トイレ,abc,1,2,def'

const CSV2 =
'menu,title,lat,lng,content\n' +
'白浜,円月島,76.54,32.1,これは円月島です\n' +
'公衆トイレ,xyz,3,4,wu'

it('should return 4 files', done => {
test(CSV1, CSV2)
.pipe(csv2json())
.pipe(assert.length(4))
.pipe(assert.end(done))
})
})
})
})

0 comments on commit 069f9ec

Please sign in to comment.