fix-set
v0.3.4
Published
Lets you define prefix and suffix rules to test strings against.
Downloads
9
Readme
fix-set
Description
fix-set
module lets you define simple prefix, suffix and exception rules and test strings against those rules.
Possible use cases:
- Filter query parameters from a web form.
- Filter custom HTTP header fields.
Rule Priority
- Rules:
exclude
>include
- Inside Rules:
except
>elements
>exceptPrefixes
orexceptSuffixes
>prefixes
orsuffixes
- Prefixes and suffixes has
or
relation. If a string satisfies one of them, rule is satisfied. - If no
prefixes
andsuffixes
provided, it is assumed all strings are included in rule exceptexceptPrefixes
andexceptSuffixes
.
Synopsis
TypeScript
import FixSet, { RuleConfig, FixSetConfig } from 'fix-set';
JavaScript
import FixSet from 'fix-set';
// Whitelist: Include only strings starting with 'q' but not 'qX'.
const fixSet = new FixSet({
include: {
prefixes: 'q',
exceptPrefixes: 'qx',
replacePrefix: true,
replaceSuffix: true
}
});
const name = fixSet.getName('qMemberName'); // 'MemberName'
const has = fixSet.has('qMemberName'); // true
const otherField = fixSet.getName('qxOther'); // undefined
const otherHas = fixSet.has('qxOther'); // false
// Blacklist: Include all strings excluding which begins with 'q',
// but include strings beginning with 'qX' even they also begin with 'q'.
const fixSet = new FixSet({
exclude: {
prefixes: 'q',
exceptPrefixes: 'qx',
replacePrefix: true,
replaceSuffix: true
}
});
const name = fixSet.getName('qMemberName'); // undefined
const has = fixSet.has('qMemberName'); // false
const otherField = fixSet.getName('qxOther'); // Other
const otherHas = fixSet.has('qxOther'); // true
// Usage with Array#filter, Array#map etc.
// Get included field names.
const parameters = Object.keys(formParameters).filter(param => fixSet.has(param));
const dbFields = Object.keys(formParameters)
.map(param => fixSet.getName(param))
.filter(field => field !== undefined);
// Usage with lodash.
import lodash from 'lodash';
const filteredObject = lodash.pickBy(data, (value, key) => fixSet.has(key));
// Cover only strings starting with 'q' or /^=(.+?)=/.
const fixSet = new FixSet({
include: {
prefixes: ['q', /^=(.+?)=/],
replacePrefix: true,
replaceSuffix: true
}
});
const name = fixSet.getName('qMemberName'); // 'MemberName'
const has = fixSet.has('qMemberName'); // true
const has = fixSet.has('=eq=MemberName'); // true
const has = fixSet.getName('=eq=MemberName'); // 'MemberName'
Why both include
and exclude
?
Consider two scenarios below:
- Include all strings, but not starting with 'q'. However include starting with 'qx':
{ exclude: { prefixes: 'q', exceptPrefixes: 'qx' } }
- Exclude all strings, but not starting with 'q'. However exclude starting with 'qx'
{ include: { prefixes: 'q', exceptPrefixes: 'qx' } }
API
Classes
Typedefs
FixSet
Kind: global class
- FixSet
- new FixSet([config])
- .getName(element, [options]) ⇒ string | undefined
- .has(element) ⇒ boolean
new FixSet([config])
| Param | Type | Description | | --- | --- | --- | | [config] | FixSetConfig | Configuration. |
fixSet.getName(element, [options]) ⇒ string | undefined
Kind: instance method of FixSet
Returns: string | undefined -
| Param | Type | Default | Description | | --- | --- | --- | --- | | element | string | | Element name to test whether it is covered by rule. | | [options] | Object | {} | Options | | [options.replacePrefix] | boolean | undefined | | Whether it should prefix be stripped from start of field name. Defaults to value given during object cunstruction. | | [options.replaceSuffix] | boolean | undefined | | Whether it should suffix be stripped from end of field name. Defaults to value given during object cunstruction. |
fixSet.has(element) ⇒ boolean
Kind: instance method of FixSet
Returns: boolean -
| Param | Type | Description | | --- | --- | --- | | element | string | Element name to test. |
FixSetConfig : Object
Kind: global typedef
Properties
| Name | Type | Description | | --- | --- | --- | | include | RuleConfig | Configuration rules for included fields. | | exclude | RuleConfig | Configuration rules for excluded fields. |
RuleConfig : Object
Kind: global typedef
Properties
| Name | Type | Description | | --- | --- | --- | | elements | string | Array.<string> | Set.<string> | Strings which are covered by rule. They are compared by equal operator. | | except | string | Array.<string> | Set.<string> | Fields which are not covered by rule. | | prefixes | string | RegExp | Array.<(string|RegExp)> | Set.<(string|RegExp)> | Strings which starts with given prefixes are covered by rule. | | suffixes | string | RegExp | Array.<(string|RegExp)> | Set.<(string|RegExp)> | Strings which ends with given suffixes are covered by rule. | | exceptPrefixes | string | RegExp | Array.<(string|RegExp)> | Set.<(string|RegExp)> | Strings which starts with given prefixes are NOT covered by rule. | | exceptSuffixes | string | RegExp | Array.<(string|RegExp)> | Set.<(string|RegExp)> | Strings which ends with given suffixes are NOT covered by rule. | | replacePrefix | boolean | Whether it should prefix be stripped from start of field name | | replaceSuffix | boolean | Whether it should suffix be stripped from end of field name. |