Comparing version 0.1.4 to 0.1.5
354
index.js
@@ -34,6 +34,10 @@ /** | ||
file.pathSepRegex = /[\/\\]/g; | ||
file.normalizeSlash =function(str) { | ||
file.normalizeSlash = function(str) { | ||
return str.replace(file.pathSepRegex, '/'); | ||
}; | ||
file.escapeRegex = function(re) { | ||
return re.replace(/(.)/g, '\\$1'); | ||
}; | ||
// Default encoding | ||
@@ -59,13 +63,10 @@ file.encoding = function(options) { | ||
// 'foo/bar/baz/quux/file.ext' => 'file.ext' | ||
// 'foo/bar/baz/quux' => 'quux' | ||
// 'foo/bar/baz/quux/' => '' | ||
file.lastSegment = function() { | ||
var filepath = file.normalizeSlash(path.join.apply(null, arguments)); | ||
return filepath.split('/').pop(); | ||
}; | ||
/** | ||
* CWD | ||
*/ | ||
// Set the CWD | ||
// Normalized path to the CWD | ||
// @example: file.cwd('foo') | ||
file.cwd = function() { | ||
var filepath = path.join.apply(null, arguments); | ||
var filepath = path.join.apply(path, arguments); | ||
return file.normalizeSlash(path.join(cwd, filepath)); | ||
@@ -76,24 +77,80 @@ }; | ||
file.setCWD = function() { | ||
var filepath = path.join.apply(null, arguments); | ||
var filepath = path.join.apply(path, arguments); | ||
process.chdir(filepath); | ||
}; | ||
file.filename = function() { | ||
var filepath = path.join.apply(null, arguments); | ||
return path.basename(filepath); | ||
/** | ||
* Path | ||
*/ | ||
// The last segment of a filepath | ||
file.lastSegment = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
return _.compact(filepath.split(path.sep)).pop(); | ||
}; | ||
file.name = function() { | ||
var filepath = path.join.apply(null, arguments); | ||
// The last segment of a filepath | ||
file.firstSegment = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
return _.compact(filepath.split(path.sep)).slice(0, 1)[0]; | ||
}; | ||
file.firstDir = file.firstSegment; | ||
// Directory path | ||
file.dirname = function() { | ||
var filepath = path.join.apply(path, arguments).split(path.sep); | ||
var dirlen = filepath.length - 1; | ||
var dir = file.normalizeSlash(filepath.splice(0, dirlen).join(path.sep)); | ||
return file.addTrailingSlash(dir); | ||
}; | ||
// Directory path | ||
file.dir = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
if(file.endsWith(filepath, path.extname(filepath))) { | ||
filepath = file.removeFilename(filepath); | ||
return filepath; | ||
} | ||
return filepath; | ||
}; | ||
// Last dictory path segment, excluding the filename | ||
file.lastDir = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
if(file.hasExt(file.lastSegment(filepath))) { | ||
filepath = file.removeFilename(filepath); | ||
} | ||
var segments = file.dir(filepath).split(path.sep); | ||
// return _.compact(segments).splice(-1,1)[0]; | ||
return _.compact(segments).pop(); | ||
}; | ||
// The last character in a filepath. 'foo/bar/baz/' => '/' | ||
file.lastChar = function(filepath) { | ||
return _.toArray(filepath).pop(); | ||
}; | ||
// Returns a filename | ||
file.filename = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
var re = /[\w.-]+$/; | ||
return re.exec(filepath)[0]; | ||
if(re.exec(filepath)) { | ||
return re.exec(filepath)[0]; | ||
} else { | ||
return ''; | ||
} | ||
}; | ||
file.ext = function() { | ||
var filepath = path.join.apply(null, arguments); | ||
return path.extname(filepath); | ||
// Strip the filename from a file path | ||
file.removeFilename = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
if(file.hasExt(file.lastSegment(filepath))) { | ||
filepath = filepath.replace(/[^\/|\\]*$/, ''); | ||
} | ||
return filepath; | ||
}; | ||
file.basename = function() { | ||
var filepath = path.join.apply(null, arguments); | ||
var filepath = path.join.apply(path, arguments); | ||
return path.basename(filepath, path.extname(filepath)); | ||
@@ -104,3 +161,3 @@ }; | ||
file.base = function() { | ||
var filepath = path.join.apply(null, arguments); | ||
var filepath = path.join.apply(path, arguments); | ||
var name = path.basename(filepath, path.extname(filepath)); | ||
@@ -110,20 +167,67 @@ return name.split('.')[0]; | ||
// Filename without extension | ||
file.rename = function(dir, filename) { | ||
return file.addTrailingSlash(path.join(dir, filename)); | ||
// File extension without the dot | ||
file.ext = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
return path.extname(filepath).replace(/\./, ''); | ||
}; | ||
// Retrieve a specific file using globbing patterns. If | ||
// multiple matches are found, only the first is returned | ||
file.getFile = function(filepath, options) { | ||
var str = file.expandFiles(filepath, options)[0]; | ||
return str ? String(str) : null; | ||
// Get the _last_ file extension. | ||
// @example 'foo/bar/file.tmpl.md' => 'md' | ||
file.lastExt = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
var sep = file.escapeRegex(path.sep); | ||
var ext = new RegExp('^.*?\\.([^.|' + sep + ']*)$', 'g'); | ||
var segments = ext.exec(filepath); | ||
return segments && segments[1].length > 0 ? segments[1] : ''; | ||
}; | ||
// Does the filepath actually exist? | ||
file.exists = function() { | ||
var filepath = path.join.apply(null, arguments); | ||
return fs.existsSync(filepath); | ||
// Returns true if the filepath ends in a file with an extension | ||
file.hasExt = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
var last = file.lastSegment(filepath); | ||
return /\./.test(last); | ||
}; | ||
// Returns true if the filepath ends with the suffix | ||
file.endsWith = function(filepath, suffix) { | ||
filepath = path.normalize(filepath); | ||
suffix = path.normalize(suffix); | ||
return filepath.indexOf(suffix, filepath.length - suffix.length) !== -1; | ||
}; | ||
// Return a list of files with the given extension. | ||
file.withExt = function (filepath, ext) { | ||
var files = fs.readdirSync(filepath); | ||
var list = []; | ||
files.forEach(function (filename) { | ||
if (file.endsWith(filename, ext)) { | ||
list.push(filename); | ||
} | ||
}); | ||
return list; | ||
}; | ||
// Add a trailing slash to the file path | ||
file.addTrailingSlash = function () { | ||
var filepath = path.join.apply(path, arguments); | ||
if (filepath.charAt(filepath.length - 1) !== path.sep) { | ||
if(!file.hasExt(filepath) && !file.isFile(filepath)) { | ||
filepath += path.sep; | ||
} | ||
} | ||
return filepath; | ||
}; | ||
// Remove the trailing slash from a file path | ||
file.removeTrailingSlash = function () { | ||
var filepath = path.join.apply(path, arguments); | ||
var sep = new RegExp(file.escapeRegex(path.sep) + '$'); | ||
return filepath.replace(sep, ''); | ||
}; | ||
/** | ||
* Read Files | ||
*/ | ||
// Read file synchronously | ||
@@ -136,3 +240,3 @@ file.readFileSync = function(filepath, options) { | ||
} catch (err) { | ||
err.message = 'Failed to read "' + filepath + '": ' + err.code; | ||
err.message = 'Failed to read "' + filepath + '": ' + err.message; | ||
throw err; | ||
@@ -142,2 +246,3 @@ } | ||
// Read JSON file synchronously and parse content as JSON | ||
@@ -171,3 +276,3 @@ file.readJSONSync = function(filepath) { | ||
*/ | ||
file.readDataFile = function(filepath, options) { | ||
file.readDataSync = function(filepath, options) { | ||
options = options || {}; | ||
@@ -205,5 +310,5 @@ var ext = path.extname(filepath); | ||
if(options.namespace === true) { | ||
obj[name] = _.extend(obj, file.readDataFile(fp)); | ||
obj[name] = _.extend(obj, file.readDataSync(fp)); | ||
} else { | ||
obj = _.extend(obj, file.readDataFile(fp)); | ||
obj = _.extend(obj, file.readDataSync(fp)); | ||
} | ||
@@ -216,2 +321,6 @@ } | ||
/** | ||
* Make directories | ||
*/ | ||
file.mkdir = function (dest, callback) { | ||
@@ -232,10 +341,100 @@ var destpath = path.dirname(dest); | ||
// don't already exist | ||
file.mkdirSync = function (dir, mode) { | ||
file.mkdirSync = function (dirpath, mode) { | ||
mode = mode || parseInt('0777', 8) & (~process.umask()); | ||
if (!fs.existsSync(dir)) { | ||
file.mkdirSync(path.dirname(dir), mode); | ||
fs.mkdirSync(dir, mode); | ||
if (!fs.existsSync(dirpath)) { | ||
var parentDir = path.dirname(dirpath); | ||
if (fs.existsSync(parentDir)) { | ||
fs.mkdirSync(dirpath, mode); | ||
} else { | ||
file.mkdirSync(parentDir); | ||
fs.mkdirSync(dirpath, mode); | ||
} | ||
} | ||
}; | ||
// Testing out the `mkdirp` lib as an alternative to | ||
// built-in mkdir functions. | ||
file.mkdirp = function (dir) { | ||
require('mkdirp')(dir, function (err) { | ||
if (err) {console.error(err); } | ||
}); | ||
}; | ||
file.mkdirpSync = function (dir) { | ||
require('mkdirp').sync(dir); | ||
}; | ||
/** | ||
* Remove | ||
*/ | ||
// Remove any directories and child directories that exist | ||
file.rmdirSync = function () { | ||
var dirpath = path.join.apply(path, arguments); | ||
if (fs.existsSync(dirpath)) { | ||
var files = fs.readdirSync(dirpath); | ||
for (var i = 0, l = files.length; i < l; i++) { | ||
var filepath = path.join(dirpath, files[i]); | ||
if (filepath === "." || filepath === "..") { | ||
continue; | ||
} else if (fs.statSync(filepath).isDirectory()) { | ||
file.rmdirSync(filepath); | ||
} else { | ||
fs.unlinkSync(filepath); | ||
} | ||
} | ||
fs.rmdirSync(dirpath); | ||
} | ||
}; | ||
// Just testing these two signatures. My thinking | ||
// is that a try catch might be slightly faster | ||
// here, and potentially avoid race conditions. | ||
file.rmdirSync2 = function () { | ||
var files, dirpath = path.join.apply(path, arguments); | ||
try { | ||
files = fs.readdirSync(dirpath); | ||
} catch (err) { | ||
return; | ||
} | ||
if (files.length > 0) { | ||
for (var i = 0, l = files.length; i < l; i++) { | ||
var filepath = path.join(dirpath, files[i]); | ||
if (filepath === "." || filepath === "..") { | ||
continue; | ||
} else if (fs.statSync(filepath).isDirectory()) { | ||
file.rmdirSync(filepath); | ||
} else { | ||
fs.unlinkSync(filepath); | ||
} | ||
} | ||
} | ||
fs.rmdirSync(dirpath); | ||
}; | ||
file.rmdir = function (dirpath, callback) { | ||
if (!_.isFunction(callback)) {callback = function () {};} | ||
fs.readdir(dirpath, function (err, files) { | ||
if (err) {return callback(err);} | ||
require('async').each(files, function (segment, next) { | ||
var dirpath = path.join(dirpath, segment); | ||
fs.stat(dirpath, function (err, stats) { | ||
if (err) {return callback(err); } | ||
if (stats.isDirectory()) { | ||
file.rmdir(dirpath, next); | ||
} else { | ||
fs.unlink(dirpath, next); | ||
} | ||
}); | ||
}, function () { | ||
fs.rmdir(dirpath, callback); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Write | ||
*/ | ||
file.writeFile = function (dest, content, callback) { | ||
@@ -255,26 +454,48 @@ var destpath = path.dirname(dest); | ||
// Write files to disk, synchronously | ||
file.writeFileSync = function(src, content, options) { | ||
file.writeFileSync = function(dest, content, options) { | ||
options = options || {}; | ||
var dirpath = path.dirname(src); | ||
var dirpath = path.dirname(dest); | ||
if (!file.exists(dirpath)) { | ||
file.mkdirSync(dirpath); | ||
} | ||
fs.writeFileSync(src, content, file.encoding(options)); | ||
fs.writeFileSync(dest, content, file.encoding(options)); | ||
}; | ||
file.writeJSONSync = function(src, content, options) { | ||
file.writeJSONSync = function(dest, content, options) { | ||
options = options || {}; | ||
options.indent = options.indent || 2; | ||
content = JSON.stringify(content, null, options.indent); | ||
file.writeFileSync(src, content); | ||
file.writeFileSync(dest, content); | ||
}; | ||
file.writeYAMLSync = function(src, content, options) { | ||
file.writeYAMLSync = function(dest, content, options) { | ||
options = options || {}; | ||
options.indent = options.indent || 2; | ||
content = YAML.dump(content, null, options.indent); | ||
file.writeFileSync(src, content); | ||
file.writeFileSync(dest, content); | ||
}; | ||
// @example: file.writeDataFile('foo.yaml', {name: "Foo"}); | ||
file.writeDataSync = function(dest, content, options) { | ||
options = options || {}; | ||
var ext = path.extname(dest); | ||
var writer = file.writeJSONSync; | ||
switch(ext) { | ||
case '.json': | ||
writer = file.writeJSONSync; | ||
break; | ||
case '.yml': | ||
case '.yaml': | ||
writer = file.writeYAMLSync; | ||
break; | ||
} | ||
return writer(dest, content, ext); | ||
}; | ||
/** | ||
* Copy | ||
*/ | ||
// Copy files synchronously from a to b. | ||
file.copyFileSync = function (src, dest, options) { | ||
@@ -287,2 +508,28 @@ options = options || {}; | ||
/** | ||
* Rename | ||
*/ | ||
// Filename without extension | ||
file.rename = function (from, to) { | ||
fs.rename(from, to, function (err) { | ||
if (err) {throw err; } | ||
fs.stat(to, function (err, stats) { | ||
if (err) {throw err; } | ||
console.log('stats: ' + JSON.stringify(stats)); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Checks | ||
*/ | ||
// Does the filepath actually exist? | ||
file.exists = function() { | ||
var filepath = path.join.apply(path, arguments); | ||
return fs.existsSync(filepath); | ||
}; | ||
// If the given file exists, does it have any content? | ||
@@ -312,2 +559,3 @@ file.isEmptyFile = function(filepath) { | ||
// SOURCED FROM globule: https://github.com/cowboy/node-globule | ||
@@ -362,1 +610,9 @@ // Process specified wildcard glob patterns or filenames against a | ||
}; | ||
// Retrieve a specific file using globbing patterns. If | ||
// multiple matches are found, only the first is returned | ||
file.getFile = function(filepath, options) { | ||
var str = file.expandFiles(filepath, options)[0]; | ||
return str ? String(str) : null; | ||
}; |
{ | ||
"name": "fs-utils", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Generalized fs utils for Node.js projects.", | ||
@@ -26,13 +26,14 @@ "main": "index.js", | ||
"dependencies": { | ||
"chalk": "~0.4.0", | ||
"cwd": "~0.1.1", | ||
"glob": "~3.2.8", | ||
"iconv-lite": "~0.2.11", | ||
"glob": "~3.2.8", | ||
"cwd": "~0.1.1", | ||
"js-yaml": "~3.0.1" | ||
"js-yaml": "~3.0.1", | ||
"lodash": "~2.4.1", | ||
"mkdirp": "~0.3.5" | ||
}, | ||
"devDependencies": { | ||
"chalk": "~0.2.0", | ||
"lodash": "~2.2.1", | ||
"mocha": "~1.17.0", | ||
"chai": "~1.8.1" | ||
"mocha": "~1.17.0" | ||
} | ||
} |
@@ -0,0 +0,0 @@ # fs-utils |
@@ -16,4 +16,6 @@ /** | ||
describe('path methods', function() { | ||
// Normalize slashes in some test results | ||
var normalize = file.normalizeSlash; | ||
describe('Normalize slashes', function() { | ||
it('should normalize slash', function() { | ||
@@ -23,23 +25,514 @@ var expected = 'foo/bar/baz'; | ||
expect(actual).to.eql(expected); | ||
expected = '/foo/bar/baz/'; | ||
actual = file.normalizeSlash('\\foo\\bar\\baz\\'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); | ||
// 'foo/bar/baz/quux/file.ext' => 'file.ext' | ||
// 'foo/bar/baz/quux' => 'quux' | ||
// 'foo/bar/baz/quux/' => '' | ||
it('should return the last segment', function() { | ||
var expected = 'file.ext'; | ||
var actual = file.lastSegment('foo/bar/baz/quux/file.ext'); | ||
expect(actual).to.eql(expected); | ||
describe('Trailing slashes', function() { | ||
describe('Add trailing slashes', function() { | ||
it('should add a trailing slash when it appears to be a directory', function() { | ||
var expected = 'foo/bar/baz/'; | ||
var actual = file.addTrailingSlash('foo/bar/baz'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = 'quux'; | ||
actual = file.lastSegment('foo/bar/baz/quux'); | ||
expect(actual).to.eql(expected); | ||
expected = '/foo/bar/baz/'; | ||
actual = file.addTrailingSlash('/foo/bar/baz'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = ''; | ||
actual = file.lastSegment('foo/bar/baz/quux/'); | ||
expect(actual).to.eql(expected); | ||
expected = 'foo/bar.baz/quux/'; | ||
actual = file.addTrailingSlash('./foo/bar.baz/quux'); | ||
expect(normalize(actual)).to.eql(expected); | ||
}); | ||
expected = 'foo/bar/baz/'; | ||
actual = file.addTrailingSlash('./foo/bar/baz'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = '/foo/bar/baz/'; | ||
actual = file.addTrailingSlash('\\foo\\bar\\baz'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = 'foo/bar/baz/'; | ||
actual = file.addTrailingSlash('foo\\bar\\baz'); | ||
expect(normalize(actual)).to.eql(expected); | ||
}); | ||
it('should not add a trailing slash when it already has one', function() { | ||
var expected = 'foo/bar/baz/'; | ||
var actual = file.addTrailingSlash('foo/bar/baz/'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = '/foo/bar/baz/'; | ||
actual = file.addTrailingSlash('/foo/bar/baz/'); | ||
expect(normalize(actual)).to.eql(expected); | ||
}); | ||
it('should not add a trailing slash when it appears to be a file', function() { | ||
var expected = 'foo/bar/baz.md'; | ||
var actual = file.addTrailingSlash('./foo/bar/baz.md'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = '/foo/bar/baz.md'; | ||
actual = file.addTrailingSlash('/foo/bar/baz.md'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = '/foo/bar/baz.md'; | ||
actual = file.addTrailingSlash('\\foo\\bar\\baz.md'); | ||
expect(normalize(actual)).to.eql(expected); | ||
}); | ||
}); | ||
describe('Remove trailing slashes', function() { | ||
it('should remove a trailing slash from the given file path', function() { | ||
var expected = 'one/two'; | ||
var actual = file.removeTrailingSlash('./one/two/'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = '/three/four/five'; | ||
actual = file.removeTrailingSlash('/three/four/five/'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = '/six/seven/eight'; | ||
actual = file.removeTrailingSlash('\\six\\seven\\eight\\'); | ||
expect(normalize(actual)).to.eql(expected); | ||
}); | ||
}); | ||
}); | ||
describe('endsWith', function() { | ||
it('should return true if the path ends with the given string', function() { | ||
var expected = true; | ||
var actual = file.endsWith('foo\\bar\\baz\\', '/'); | ||
expect(actual).to.eql(expected); | ||
expected = true; | ||
actual = file.endsWith('foo\\bar\\baz\\', '\\'); | ||
expect(actual).to.eql(expected); | ||
expected = true; | ||
actual = file.endsWith('foo/bar/baz/', '/'); | ||
expect(actual).to.eql(expected); | ||
expected = true; | ||
actual = file.endsWith('foo\\bar\\baz.md', 'baz.md'); | ||
expect(actual).to.eql(expected); | ||
expected = true; | ||
actual = file.endsWith('foo\\bar\\baz.md', '.md'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
it('should return false if the path does not end with the given string', function() { | ||
var expected = false; | ||
var actual = file.endsWith('foo\\bar\\baz.md', '/'); | ||
expect(actual).to.eql(expected); | ||
expected = false; | ||
actual = file.endsWith('foo\\bar\\baz.md', 'baz'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); | ||
describe('lastExt', function() { | ||
it('should return the last file extension', function() { | ||
var expected = 'md'; | ||
var actual = file.lastExt('foo/bar/baz/quux.bar/file.tmpl.md'); | ||
expect(actual).to.eql(expected); | ||
expected = 'md'; | ||
actual = file.lastExt('./foo/bar/baz/quux.bar/file.tmpl.md'); | ||
expect(actual).to.eql(expected); | ||
expected = ''; | ||
actual = file.lastExt('foo/bar/baz/quux.bar/CHANGELOG'); | ||
expect(actual).to.eql(expected); | ||
expected = 'gitignore'; | ||
actual = file.lastExt('/foo/bar/baz/quux.bar/.gitignore'); | ||
expect(actual).to.eql(expected); | ||
expected = 'html'; | ||
actual = file.lastExt('./foo/bar/baz/quux/index.html'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); | ||
describe('withExt', function() { | ||
it('should return files from specified directory that end with the given file extension.', function() { | ||
var expected = ['test.txt']; | ||
var actual = file.withExt('./test/fixtures', 'txt'); | ||
expect(actual).to.eql(expected); | ||
expected = ['fs-tests.js', 'path-tests.js']; | ||
actual = file.withExt('./test', 'js'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); | ||
describe('firstSegment', function() { | ||
it('should return the first segment in the given file path', function() { | ||
var expected = 'apple'; | ||
var actual = file.firstSegment('apple/orange/file.ext'); | ||
expect(actual).to.eql(expected); | ||
expected = 'grape'; | ||
actual = file.firstSegment('/grape/watermelon/quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'banana'; | ||
actual = file.firstSegment('./banana/strawberry/quux/'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); | ||
describe('lastSegment', function() { | ||
it('should return the last segment in the given file path', function() { | ||
var expected = 'file.ext'; | ||
var actual = file.lastSegment('square/rectangle/file.ext'); | ||
expect(actual).to.eql(expected); | ||
expected = 'four'; | ||
actual = file.lastSegment('one/two/three/four'); | ||
expect(actual).to.eql(expected); | ||
expected = 'grape'; | ||
actual = file.lastSegment('apple/orange/grape/'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); | ||
describe('removeFilename', function() { | ||
it('should remove the filename from the given file path', function() { | ||
var expected = 'square/rectangle/'; | ||
var actual = file.removeFilename('square/rectangle/file.ext'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = 'one/two/three/four'; | ||
actual = file.removeFilename('one/two/three/four'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = 'apple/orange/grape/'; | ||
actual = file.removeFilename('apple/orange/grape/'); | ||
expect(normalize(actual)).to.eql(expected); | ||
}); | ||
}); | ||
describe('dirname', function() { | ||
it('should return the dirname in the given file path', function() { | ||
var expected = 'square/rectangle/'; | ||
var actual = file.dirname('square/rectangle/file.ext'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = 'one/two/three/'; | ||
actual = file.dirname('one/two/three/four'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = 'apple/orange/grape/'; | ||
actual = file.dirname('apple/orange/grape/'); | ||
expect(normalize(actual)).to.eql(expected); | ||
}); | ||
it('should return the directory in the given file path, including filenames that do not have an extensions.', function() { | ||
var expected = 'square/rectangle/'; | ||
var actual = file.dir('square/rectangle/file.ext'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = 'one/two/three/four'; | ||
actual = file.dir('one/two/three/four'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = 'one/two/three/CHANGELOG'; | ||
actual = file.dir('one/two/three/CHANGELOG'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = 'apple/orange/grape/'; | ||
actual = file.dir('./apple/orange/grape/'); | ||
expect(normalize(actual)).to.eql(expected); | ||
}); | ||
}); | ||
describe('firstDir', function() { | ||
it('should return the first directory in the given file path (alias for firstSegment)', function() { | ||
var expected = 'apple'; | ||
var actual = file.firstDir('apple/orange/file.ext'); | ||
expect(actual).to.eql(expected); | ||
expected = 'grape'; | ||
actual = file.firstDir('/grape/watermelon/quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'banana'; | ||
actual = file.firstDir('./banana/strawberry/quux/'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); | ||
describe('lastDir', function() { | ||
it('should return the last dirname in the given file path', function() { | ||
var expected = 'rectangle'; | ||
var actual = file.lastDir('square/rectangle/file.ext'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = 'four'; | ||
actual = file.lastDir('one/two/three/four'); | ||
expect(normalize(actual)).to.eql(expected); | ||
expected = 'grape'; | ||
actual = file.lastDir('apple/orange/grape/'); | ||
expect(normalize(actual)).to.eql(expected); | ||
}); | ||
}); | ||
describe('lastChar:', function() { | ||
it('should return the last character in the given file path', function() { | ||
var expected = 't'; | ||
var actual = file.lastChar('foo/bar/baz/quux/file.ext'); | ||
expect(actual).to.eql(expected); | ||
expected = 'x'; | ||
actual = file.lastChar('foo/bar/baz/quux'); | ||
expect(actual).to.eql(expected); | ||
expected = '/'; | ||
actual = file.lastChar('foo/bar/baz/quux/'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); | ||
describe('path.basename:', function () { | ||
it('should get the basename using the Node.js path module', function () { | ||
var expected = 'file.json'; | ||
var actual = path.basename('path/to/file.json'); | ||
expect(actual).to.eql(expected); | ||
expected = 'file.tmpl.md'; | ||
actual = path.basename('path/to/file.tmpl.md'); | ||
expect(actual).to.eql(expected); | ||
expected = 'file'; | ||
actual = path.basename('path/to/file'); | ||
expect(actual).to.eql(expected); | ||
expected = 'baz.quux'; | ||
actual = path.basename('.foo.bar/baz.quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'baz.quux.'; | ||
actual = path.basename('.foo.bar/baz.quux.'); | ||
expect(actual).to.eql(expected); | ||
expected = '.html'; | ||
actual = path.basename('.html'); | ||
expect(actual).to.eql(expected); | ||
expected = 'foo.bar.baz.quux'; | ||
actual = path.basename('/foo.bar.baz.quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = path.basename('/foo/bar/baz/asdf/quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux.html'; | ||
actual = path.basename('/foo/bar/baz/asdf/quux.html'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = path.basename('/foo/bar/baz/quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = path.basename('/foo/bar/baz/quux/'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = path.basename('/quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = path.basename('/quux/'); | ||
expect(actual).to.eql(expected); | ||
expected = 'foo.bar.baz.quux'; | ||
actual = path.basename('foo.bar.baz.quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'baz.quux'; | ||
actual = path.basename('foo.bar/baz.quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'bar.baz.quux'; | ||
actual = path.basename('foo/bar.baz.quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'bar.baz.quux'; | ||
actual = path.basename('foo/bar.baz.quux/'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = path.basename('foo/bar.baz/quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'baz.quux'; | ||
actual = path.basename('foo/bar/baz.quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'baz.quux.'; | ||
actual = path.basename('foo/bar/baz.quux.'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = path.basename('foo/bar/baz/quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = path.basename('foo/bar/baz/quux/'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = path.basename('foo\\bar\\baz\\quux\\'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = path.basename('quux/'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
it('should return the name of a file, exluding directories from the result', function () { | ||
var expected = 'file.json'; | ||
var actual = file.filename('path/to/file.json'); | ||
expect(actual).to.eql(expected); | ||
expected = 'file.tmpl.md'; | ||
actual = file.filename('path/to/file.tmpl.md'); | ||
expect(actual).to.eql(expected); | ||
expected = 'file'; | ||
actual = file.filename('path/to/file'); | ||
expect(actual).to.eql(expected); | ||
expected = 'baz.quux'; | ||
actual = file.filename('.foo.bar/baz.quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'baz.quux.'; | ||
actual = file.filename('.foo.bar/baz.quux.'); | ||
expect(actual).to.eql(expected); | ||
expected = '.html'; | ||
actual = file.filename('.html'); | ||
expect(actual).to.eql(expected); | ||
expected = 'foo.bar.baz.quux'; | ||
actual = file.filename('/foo.bar.baz.quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = file.filename('/foo/bar/baz/asdf/quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux.html'; | ||
actual = file.filename('/foo/bar/baz/asdf/quux.html'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = file.filename('/foo/bar/baz/quux'); | ||
expect(actual).to.eql(expected); | ||
// Different result than path.basename | ||
expected = ''; | ||
actual = file.filename('/foo/bar/baz/quux/'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = file.filename('/quux'); | ||
expect(actual).to.eql(expected); | ||
// Different result than path.basename | ||
expected = ''; | ||
actual = file.filename('/quux/'); | ||
expect(actual).to.eql(expected); | ||
expected = 'foo.bar.baz.quux'; | ||
actual = file.filename('foo.bar.baz.quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'baz.quux'; | ||
actual = file.filename('foo.bar/baz.quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'bar.baz.quux'; | ||
actual = file.filename('foo/bar.baz.quux'); | ||
expect(actual).to.eql(expected); | ||
// Different result than path.basename | ||
expected = ''; | ||
actual = file.filename('foo/bar.baz.quux/'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = file.filename('foo/bar.baz/quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'baz.quux'; | ||
actual = file.filename('foo/bar/baz.quux'); | ||
expect(actual).to.eql(expected); | ||
expected = 'baz.quux.'; | ||
actual = file.filename('foo/bar/baz.quux.'); | ||
expect(actual).to.eql(expected); | ||
expected = 'quux'; | ||
actual = file.filename('foo/bar/baz/quux'); | ||
expect(actual).to.eql(expected); | ||
// Different result than path.basename | ||
expected = ''; | ||
actual = file.filename('foo/bar/baz/quux/'); | ||
expect(actual).to.eql(expected); | ||
// Different result than path.basename | ||
expected = ''; | ||
actual = file.filename('foo\\bar\\baz\\quux\\'); | ||
expect(actual).to.eql(expected); | ||
// Different result than path.basename | ||
expected = ''; | ||
actual = file.filename('quux/'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); | ||
describe('file extension:', function() { | ||
it('should get the extension', function() { | ||
var expected = 'json'; | ||
var actual = file.ext('path/to/file.json'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
it('should get the basename', function() { | ||
var expected = 'file'; | ||
var actual = file.basename('path/to/file.json'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
it('should get the base without extension', function() { | ||
var expected = 'file'; | ||
var actual = file.base('path/to/file.json'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); | ||
describe('cwd:', function() { | ||
it('should get the absolute cwd', function() { | ||
@@ -65,41 +558,2 @@ var expected = file.normalizeSlash(cwd); | ||
}); | ||
it('should get the filename', function() { | ||
var expected = 'file.json'; | ||
var actual = file.filename('path/to/file.json'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
it('should get the name', function() { | ||
var expected = 'file.json'; | ||
var actual = file.name('path/to/file.json'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
it('should get the extension', function() { | ||
var expected = '.json'; | ||
var actual = file.ext('path/to/file.json'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
it('should get the basename', function() { | ||
var expected = 'file'; | ||
var actual = file.basename('path/to/file.json'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
it('should get the base without extension', function() { | ||
var expected = 'file'; | ||
var actual = file.base('path/to/file.json'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
xit('should get the "rename"?', function() { | ||
var expected = 'foo'; | ||
var actual = file.rename('path/to', 'file.json'); | ||
expect(actual).to.eql(expected); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39627
2
11
1080
7
1
+ Addedchalk@~0.4.0
+ Addedlodash@~2.4.1
+ Addedmkdirp@~0.3.5
+ Addedansi-styles@1.0.0(transitive)
+ Addedchalk@0.4.0(transitive)
+ Addedhas-color@0.1.7(transitive)
+ Addedmkdirp@0.3.5(transitive)
+ Addedstrip-ansi@0.1.1(transitive)