diff options
author | Minteck <nekostarfan@gmail.com> | 2021-08-24 14:41:48 +0200 |
---|---|---|
committer | Minteck <nekostarfan@gmail.com> | 2021-08-24 14:41:48 +0200 |
commit | d25e11bee6ca5ca523884da132d18e1400e077b9 (patch) | |
tree | 8af39fde19f7ed640a60fb397c7edd647dff1c4c /node_modules/strip-dirs/index.js | |
download | kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.tar.gz kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.tar.bz2 kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.zip |
Initial commit
Diffstat (limited to 'node_modules/strip-dirs/index.js')
-rw-r--r-- | node_modules/strip-dirs/index.js | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/node_modules/strip-dirs/index.js b/node_modules/strip-dirs/index.js new file mode 100644 index 0000000..974af89 --- /dev/null +++ b/node_modules/strip-dirs/index.js @@ -0,0 +1,72 @@ +/*! + * strip-dirs | MIT (c) Shinnosuke Watanabe + * https://github.com/shinnn/node-strip-dirs +*/ +'use strict'; + +const path = require('path'); +const util = require('util'); + +const isNaturalNumber = require('is-natural-number'); + +module.exports = function stripDirs(pathStr, count, option) { + if (typeof pathStr !== 'string') { + throw new TypeError( + util.inspect(pathStr) + + ' is not a string. First argument to strip-dirs must be a path string.' + ); + } + + if (path.posix.isAbsolute(pathStr) || path.win32.isAbsolute(pathStr)) { + throw new Error(`${pathStr} is an absolute path. strip-dirs requires a relative path.`); + } + + if (!isNaturalNumber(count, {includeZero: true})) { + throw new Error( + 'The Second argument of strip-dirs must be a natural number or 0, but received ' + + util.inspect(count) + + '.' + ); + } + + if (option) { + if (typeof option !== 'object') { + throw new TypeError( + util.inspect(option) + + ' is not an object. Expected an object with a boolean `disallowOverflow` property.' + ); + } + + if (Array.isArray(option)) { + throw new TypeError( + util.inspect(option) + + ' is an array. Expected an object with a boolean `disallowOverflow` property.' + ); + } + + if ('disallowOverflow' in option && typeof option.disallowOverflow !== 'boolean') { + throw new TypeError( + util.inspect(option.disallowOverflow) + + ' is neither true nor false. `disallowOverflow` option must be a Boolean value.' + ); + } + } else { + option = {disallowOverflow: false}; + } + + const pathComponents = path.normalize(pathStr).split(path.sep); + + if (pathComponents.length > 1 && pathComponents[0] === '.') { + pathComponents.shift(); + } + + if (count > pathComponents.length - 1) { + if (option.disallowOverflow) { + throw new RangeError('Cannot strip more directories than there are.'); + } + + count = pathComponents.length - 1; + } + + return path.join.apply(null, pathComponents.slice(count)); +}; |