config-utilities
Advanced tools
Comparing version 0.0.3 to 0.1.0
20
index.js
@@ -25,2 +25,3 @@ /* | ||
const path = require('path'); | ||
const { slash } = require('slashf'); | ||
@@ -38,5 +39,4 @@ /* | ||
resolve | ||
@param [opts] - Optional options | ||
@param [opts] - Optional settings | ||
Options | ||
@@ -46,2 +46,4 @@ @param opts.baseDir - Base folder path for relative entries resolution | ||
dependencies | ||
@param [opts.normalize = false] - True to normalize all (output) slashes to | ||
forward slashes; if false, OS conventions will apply | ||
@@ -58,3 +60,3 @@ @returns An object with the same shape as `paths` and the (resolvable) paths | ||
resolvePaths(paths) = { | ||
resolvePaths(paths) == { | ||
myFolder1: '/my/current/working/directory/full/path', | ||
@@ -67,2 +69,3 @@ myFolder2: '/home', | ||
const opts2 = { | ||
normalize: false, | ||
iterationLimit: 1000, | ||
@@ -75,3 +78,3 @@ baseDir: process.cwd(), | ||
// path in the config | ||
const paths2 = Object.fromEntries( | ||
let paths2 = Object.fromEntries( | ||
Object.entries(paths).map( | ||
@@ -102,3 +105,3 @@ ([k, v]) => [ | ||
Object.keys(paths2).forEach((pName) => { | ||
Object.keys(paths2).forEach((pName) => { // eslint-disable-line no-loop-func | ||
const p = paths2[pName]; | ||
@@ -129,2 +132,9 @@ | ||
// Slash normalization | ||
if (opts2.normalize) { | ||
paths2 = Object.fromEntries( | ||
Object.entries(paths2).map(([k, v]) => [k, slash(v)]), | ||
); | ||
} | ||
return paths2; | ||
@@ -131,0 +141,0 @@ } |
{ | ||
"name": "config-utilities", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Configuration file utilities", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npx jest", | ||
"test": "npx mocha", | ||
"lint": "npx eslint *.js" | ||
@@ -23,10 +23,12 @@ }, | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"eslint": "^5.9.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"jest": "^23.6.0" | ||
"mocha": "^5.2.0" | ||
}, | ||
"dependencies": { | ||
"polyfill-object.fromentries": "^1.0.1" | ||
"polyfill-object.fromentries": "^1.0.1", | ||
"slashf": "^1.1.0" | ||
} | ||
} |
@@ -35,2 +35,3 @@ # Configuration utilities | ||
* iterationLimit: Iteration limit to avoid circular dependencies (*defaults to 1000; only needed for huge configuration objects*) | ||
* normalize: Normalize returned slashes to forward-slashes | ||
@@ -37,0 +38,0 @@ ## License |
60
test.js
@@ -0,7 +1,11 @@ | ||
const os = require('os'); | ||
const path = require('path'); | ||
const { expect } = require('chai'); | ||
const { resolvePaths } = require('.'); | ||
/* eslint-env node, jest */ | ||
const WIN32 = os.platform() === 'win32'; | ||
test('resolvePaths()', () => { | ||
/* eslint-env mocha */ | ||
describe('resolvePaths()', () => { | ||
const paths = { | ||
@@ -14,22 +18,44 @@ myFolder1: '.', | ||
expect(resolvePaths(paths)).toEqual({ | ||
myFolder1: path.resolve(process.cwd()), | ||
myFolder2: path.resolve('/home'), | ||
specialFile: path.resolve('/home/special.txt'), | ||
myFile: path.resolve(process.cwd(), 'hey'), | ||
it('should return the expected values', () => { | ||
expect(resolvePaths(paths)).to.deep.equal({ | ||
myFolder1: path.resolve(process.cwd()), | ||
myFolder2: path.resolve('/home'), | ||
specialFile: path.resolve('/home/special.txt'), | ||
myFile: path.resolve(process.cwd(), 'hey'), | ||
}); | ||
}); | ||
const baseDir = '/my/base/dir'; | ||
it('should respect the baseDir option', () => { | ||
const baseDir = '/my/base/dir'; | ||
expect(resolvePaths(paths, { baseDir })).toEqual({ | ||
myFolder1: path.resolve(baseDir), | ||
myFolder2: path.resolve('/home'), | ||
specialFile: path.resolve('/home/special.txt'), | ||
myFile: path.resolve(baseDir, 'hey'), | ||
expect(resolvePaths(paths, { baseDir })).to.deep.equal({ | ||
myFolder1: path.resolve(baseDir), | ||
myFolder2: path.resolve('/home'), | ||
specialFile: path.resolve('/home/special.txt'), | ||
myFile: path.resolve(baseDir, 'hey'), | ||
}); | ||
}); | ||
expect(() => resolvePaths({ | ||
...paths, | ||
specialFile2: '$myFolder/nothing', | ||
}, { iterationLimit: 1 })).toThrow(); | ||
it('should throw if an iteration limit is exceeded', () => { | ||
expect(() => resolvePaths({ | ||
...paths, | ||
specialFile2: '$myFolder/nothing', | ||
}, { iterationLimit: 1 })).to.throw(); | ||
}); | ||
describe('Path normalization', () => { | ||
const allForwardSlashes = ({ a }) => !a.includes('\\'); | ||
if (WIN32) { | ||
it('should respect the normalize option', () => { | ||
expect( | ||
resolvePaths({ a: '/a/b' }), | ||
).not.to.satisfy(allForwardSlashes); | ||
expect( | ||
resolvePaths({ a: '/a/b' }, { normalize: true }), | ||
).to.satisfy(allForwardSlashes); | ||
}); | ||
} else it('(Windows-specific test)'); | ||
}); | ||
}); |
8940
162
40
2
5
+ Addedslashf@^1.1.0
+ Addedslashf@1.1.0(transitive)