package/package.json000644 000765 000024 0000000526 13050465652013026 0ustar00000000 000000 { "name": "cached-path-relative", "description": "Memoize the results of the path.relative function", "repository": "git://github.com/ashaffer/cached-path-relative.git", "version": "1.0.1", "license": "MIT", "main": "lib/index.js", "dependencies": {}, "devDependencies": { "tape": "^4.2.0", "standard": "^4.0.0" } } package/.npmignore000644 000765 000024 0000000023 12703520520012515 0ustar00000000 000000 node_modules *.log package/shim.js000644 000765 000024 0000000222 12703520775012031 0ustar00000000 000000 /** * Modules */ var path = require('path') var cachedPathRelative = require('.') /** * Install shim */ path.relative = cachedPathRelative package/History.md000644 000765 000024 0000000000 12703520520012474 0ustar00000000 000000 package/Makefile000644 000765 000024 0000000310 12703520520012155 0ustar00000000 000000 # # Vars # BIN = ./node_modules/.bin # # Tasks # node_modules: package.json @npm install test: node_modules @${BIN}/tape test/* validate: node_modules @${BIN}/standard .PHONY: test validate package/Readme.md000644 000765 000024 0000003720 12703522267012256 0ustar00000000 000000 # cached-path-relative [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) Memoize the results of the path.relative function. `path.relative` can be an expensive operation if it happens a lot, and its results shouldn't change for the same arguments. ## Installation $ npm install cached-path-relative ## Usage Use it just like your normal path.relative, but it's memoized: ```javascript var relative = require('cached-path-relative') relative('test/index.js', '.') === '../..' ``` ## Shim If you want to shim path.relative globally so that you can optimize things that don't know about this module (browserify rebuild times are a good use-case here), you can just import the shim: `require('cached-path-relative/shim')` This will globally shim `path.relative` so that its results are cached. ## License The MIT License Copyright © 2016, Weo.io <info@weo.io> 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/lib/index.js000644 000765 000024 0000001207 13050465612012744 0ustar00000000 000000 /** * Modules */ var path = require('path') /** * Vars */ var relative = path.relative var lastCwd = process.cwd() var cache = {} /** * Expose cachedPathRelative */ module.exports = cachedPathRelative /** * cachedPathRelative */ function cachedPathRelative (from, to) { // If the current working directory changes, we need // to invalidate the cache var cwd = process.cwd() if (cwd !== lastCwd) { cache = {} lastCwd = cwd } if (cache[from] && cache[from][to]) return cache[from][to] var result = relative.call(path, from, to) cache[from] = cache[from] || {} cache[from][to] = result return result } package/test/index.js000644 000765 000024 0000000510 12703521275013153 0ustar00000000 000000 /** * Imports */ var relative = require('..') var path = require('path') var test = require('tape') /** * Tests */ test('should work', function (t) { t.equal(relative('test/index.js', '.'), path.relative('test/index.js', '.')) t.equal(relative('test/index.js', '.'), path.relative('test/index.js', '.')) t.end() })