Skip to content

Commit

Permalink
[File][xs]: normalize file name by trimming, replacing spaces with '-…
Browse files Browse the repository at this point in the history
…' and lowercasing - refs datahubio/datahub-v2-pm#141
  • Loading branch information
anuveyatsu committed Apr 24, 2018
1 parent 8faa87d commit ffbbe92
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,15 @@ const parsePath = (path_, basePath = null, format = null) => {
}

const extension = path.extname(fileName)
fileName = fileName.replace(extension, '')
.toLowerCase()
.trim()
.replace(/&/g, '-and-')
.replace(/[^a-z0-9-._]+/g, '-')
const descriptor = {
path: path_,
pathType: isItUrl ? 'remote' : 'local',
name: fileName.replace(extension, ''),
name: fileName,
format: format ? format : extension.slice(1).toLowerCase()
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/some file.name.ext
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Content
8 changes: 7 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ test('parsePath function with remote url without conventional filename', t => {
const res = data.parsePath(path_)
t.is(res.path, path_)
t.is(res.pathType, 'remote')
t.is(res.name, 'NY.GDP.MKTP')
t.is(res.name, 'ny.gdp.mktp')
t.is(res.format, 'csv')
t.is(res.mediatype, undefined)
})
Expand Down Expand Up @@ -331,6 +331,12 @@ test('File class for addSchema method', async t => {
t.deepEqual(headers, ['number', 'string', 'boolean'])
})

test('File name has spaces and dots', async t => {
let path_ = 'test/fixtures/some file.name.ext'
let file = data.File.load(path_)
t.is(file.descriptor.name, 'some-file.name')
})

// ====================================
// Dataset class
// ====================================
Expand Down

0 comments on commit ffbbe92

Please sign in to comment.