package/package.json000644 001750 001750 0000001602 12455534536013032 0ustar00000000 000000 { "name" : "parents", "version" : "1.0.1", "description" : "return all the parent directories for a directory", "main" : "index.js", "bin" : {}, "directories" : { "example" : "example", "test" : "test" }, "dependencies" : { "path-platform": "~0.11.15" }, "devDependencies" : { "tap" : "~0.2.5" }, "scripts" : { "test" : "tap test/*.js" }, "repository" : { "type" : "git", "url" : "git://github.com/substack/node-parents.git" }, "homepage" : "https://github.com/substack/node-parents", "keywords" : [ "directory", "parent", "path", "tree" ], "author" : { "name" : "James Halliday", "email" : "mail@substack.net", "url" : "http://substack.net" }, "license" : "MIT", "engine" : { "node" : ">=0.6" } } package/LICENSE000644 001750 001750 0000002061 12455534155011546 0ustar00000000 000000 This software is released under the MIT license: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/index.js000644 001750 001750 0000002562 12455534351012212 0ustar00000000 000000 var pathPlatform = require('path-platform'); module.exports = function (cwd, opts) { if (cwd === undefined) cwd = process.cwd(); if (!opts) opts = {}; var platform = opts.platform || process.platform; var isWindows = /^win/.test(platform); var path = isWindows ? pathPlatform.win32 : pathPlatform; var normalize = !isWindows ? path.normalize : path.normalize('c:') === 'c:.' ? fixNormalize(path.normalize) : path.normalize; var sep = isWindows ? /[\\\/]/ : '/'; var init = isWindows ? '' : '/'; var join = function (x, y) { var ps = [ x, y ].filter(function (p) { return p && typeof p === 'string' }); return normalize(ps.join(isWindows ? '\\' : '/')); }; var res = normalize(cwd) .split(sep) .reduce(function (acc,dir,ix) { return acc.concat(join(acc[ix], dir)) }, [init]) .slice(1) .reverse() ; if (res[0] === res[1]) return [ res[0] ]; if (isWindows && /^\\/.test(cwd)) { return res.slice(0,-1).map(function (d) { var ch = d.charAt(0) return ch === '\\' ? d : ch === '.' ? '\\' + d.slice(1) : '\\' + d }); } return res; function fixNormalize(fn) { return function(p) { return fn(p).replace(/:\.$/, ':') } } } package/.travis.yml000644 001750 001750 0000000053 12455534155012651 0ustar00000000 000000 language: node_js node_js: - 0.6 - 0.8 package/example/dirname.js000644 001750 001750 0000000120 12455534155014143 0ustar00000000 000000 var parents = require('../'); var dirs = parents(__dirname); console.dir(dirs); package/example/win32.js000644 001750 001750 0000000240 12455534155013471 0ustar00000000 000000 var parents = require('../'); var dir = 'C:\\Program Files\\Maxis\\Sim City 2000\\cities'; var dirs = parents(dir, { platform : 'win32' }); console.dir(dirs); package/readme.markdown000644 001750 001750 0000003045 12455534155013545 0ustar00000000 000000 # parents Return all the parent directories of a directory, inclusive of that directory. [![build status](https://secure.travis-ci.org/substack/node-parents.png)](http://travis-ci.org/substack/node-parents) # example ## dirname ``` js var parents = require('parents'); var dirs = parents(__dirname); console.dir(dirs); ``` *** ``` [ '/home/substack/projects/node-parents/example', '/home/substack/projects/node-parents', '/home/substack/projects', '/home/substack', '/home', '/' ] ``` ## win32 ``` js var parents = require('parents'); var dir = 'C:\\Program Files\\Maxis\\Sim City 2000\\cities'; var dirs = parents(dir, { platform : 'win32' }); console.dir(dirs); ``` *** ``` [ 'C:\\Program Files\\Maxis\\Sim City 2000\\cities', 'C:\\Program Files\\Maxis\\Sim City 2000', 'C:\\Program Files\\Maxis', 'C:\\Program Files', 'C:' ] ``` # methods ``` js var parents = require('parents') ``` ## parents(dir, opts) Return an array of the parent directories of `dir`, including and starting with `dir`. If a `dir` isn't specified, `process.cwd()` will be used. Optionally specify an `opts.platform` to control whether the separator and paths works the unixy way with `'/'` or the windowsy way where sometimes things use `'/'` and sometimes they use `'\\'` and also there are leading drive letters and other exotic features. If `opts.platform` isn't specified, `process.platform` will be used. Anything that matches `/^win/` will use the windowsy behavior. # install With [npm](http://npmjs.org) do: ``` npm install parents ``` # licence MIT package/test/dirname.js000644 001750 001750 0000000603 12455534155013475 0ustar00000000 000000 var test = require('tap').test; var parents = require('../'); test('dirname', function (t) { var dirs = parents('/foo/bar/baz/quux'); t.same(dirs, [ '/foo/bar/baz/quux', '/foo/bar/baz', '/foo/bar', '/foo', '/', ]); t.end(); }); test('root', function (t) { var dirs = parents('/'); t.same(dirs, [ '/' ]); t.end(); }); package/test/win32.js000644 001750 001750 0000001621 12455534155013021 0ustar00000000 000000 var test = require('tap').test; var parents = require('../'); test('win32', function (t) { var dir = 'c:\\Program Files\\Maxis\\Sim City 2000\\cities'; var dirs = parents(dir, { platform : 'win32' }); t.same(dirs, [ 'c:\\Program Files\\Maxis\\Sim City 2000\\cities', 'c:\\Program Files\\Maxis\\Sim City 2000', 'c:\\Program Files\\Maxis', 'c:\\Program Files', 'c:', ]); t.end(); }); test('win32 c:', function (t) { var dirs = parents('c:\\', { platform : 'win32' }); t.same(dirs, [ 'c:' ]); t.end(); }); test('win32 network drive', function (t) { var dirs = parents( '\\storageserver01\\Active Projects\\ProjectA', { platform : 'win32' } ); t.same(dirs, [ '\\storageserver01\\Active Projects\\ProjectA', '\\storageserver01\\Active Projects', '\\storageserver01' ]); t.end(); });