This repository has been archived by the owner on Apr 2, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix up tests. Add test for normalizeOptions
- Loading branch information
Gabe Scholz
committed
May 14, 2016
1 parent
f1e9c80
commit 7ad3aac
Showing
3 changed files
with
84 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,19 @@ | ||
import _isArray from 'lodash/isArray' | ||
import _isString from 'lodash/isString' | ||
import _isRegExp from 'lodash/isRegExp' | ||
|
||
export const normalizeOptions = (opts = {}) => { | ||
let {include, exclude} = opts | ||
export const DEFAULT_INCLUDE = /./ | ||
export const DEFAULT_EXCLUDE = /[^a-zA-Z0-9]/ | ||
|
||
if (!_isArray(include)) { | ||
include = [include] | ||
} | ||
const toRegExp = s => _isString(s) ? new RegExp(`^${s}$`) : s | ||
const toArray = o => o ? [].concat(o) : [] | ||
|
||
if (!_isArray(exclude)) { | ||
exclude = [exclude] | ||
export const normalizeOptions = (opts = {}) => { | ||
let { | ||
include = [DEFAULT_INCLUDE], | ||
exclude = [DEFAULT_EXCLUDE] | ||
} = opts | ||
|
||
return { | ||
include: toArray(include).map(toRegExp), | ||
exclude: toArray(exclude).map(toRegExp) | ||
} | ||
|
||
include = include.map(i => { | ||
if (_isString(i)) { | ||
return new RegExp(`^${i}$`) | ||
} else if (!_isRegExp(i)) { | ||
return /./ | ||
} | ||
|
||
return i | ||
}) | ||
|
||
exclude = exclude.map(e => { | ||
if (_isString(e)) { | ||
return new RegExp(`^${e}$`) | ||
} else if (!_isRegExp(e)) { | ||
return /[^a-zA-Z0-9]/ | ||
} | ||
|
||
return e | ||
}) | ||
|
||
|
||
return {...opts, include, exclude} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {deepEqual} from 'assert' | ||
|
||
import {normalizeOptions, DEFAULT_INCLUDE, DEFAULT_EXCLUDE} from 'src/normalizeOptions' | ||
|
||
const toString = o => RegExp.prototype.toString.call(o) | ||
|
||
const convertEqual = (include, includeOutput, exclude, excludeOutput) => { | ||
const opts = normalizeOptions({include, exclude}) | ||
deepEqual(opts.include.map(toString), includeOutput.map(toString)) | ||
deepEqual(opts.exclude.map(toString), excludeOutput.map(toString)) | ||
} | ||
|
||
describe('normalizeOptions', () => { | ||
it('converts all include to an array of RegExps', () => { | ||
convertEqual('A', [/^A$/], 'B', [/^B$/]) | ||
convertEqual(['A', 'B'], [/^A$/, /^B$/], undefined, [DEFAULT_EXCLUDE]) | ||
convertEqual(/A/, [/A/], [`B`], [/^B$/]) | ||
convertEqual([/A/, 'B'], [/A/, /^B$/], undefined, [DEFAULT_EXCLUDE]) | ||
convertEqual([/A/, /B/], [/A/, /B/], undefined, [DEFAULT_EXCLUDE]) | ||
convertEqual(undefined, [DEFAULT_INCLUDE], [/A/, /B/], [/A/, /B/]) | ||
}) | ||
}) |