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
;
JavaScript
;
// Whitelist: Include only strings starting with 'q' but not 'qX'.const fixSet = include: prefixes: 'q' exceptPrefixes: 'qx' replacePrefix: true replaceSuffix: true ; const name = fixSet; // 'MemberName'const has = fixSet; // trueconst otherField = fixSet; // undefinedconst otherHas = fixSet; // false
// Blacklist: Include all strings excluding which begins with 'q',// but include strings beginning with 'qX' even they also begin with 'q'.const fixSet = exclude: prefixes: 'q' exceptPrefixes: 'qx' replacePrefix: true replaceSuffix: true ; const name = fixSet; // undefinedconst has = fixSet; // falseconst otherField = fixSet; // Otherconst otherHas = fixSet; // true
// Usage with Array#filter, Array#map etc. // Get included field names. const parameters = Object; const dbFields = Object ;
// Usage with lodash.;const filteredObject = lodash;
// Cover only strings starting with 'q' or /^=(.+?)=/.const fixSet = include: prefixes: 'q' /^==/ replacePrefix: true replaceSuffix: true ;const name = fixSet; // 'MemberName'const has = fixSet; // trueconst has = fixSet; // trueconst has = fixSet; // 'MemberName'
include
and exclude
?
Why both 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
- FixSet
Class representing a filter rule. A rule consists of prefixes, elements and disallowed elements etc. Later individual elements can be tested if they are covered by this rule.
Typedefs
- FixSetConfig :
Object
Fix rule configuration.
- RuleConfig :
Object
Fix rule options to create a fix rule from given options. Prefix and suffix parameters can be either string or regular expression. If they are provided as regular expressions, they must begin with
^
or end with$
. If noprefixes
andsuffixes
provided, it is assumed all strings are included exceptexceptPrefixes
andexceptSuffixes
.
FixSet
Class representing a filter rule. A rule consists of prefixes, elements and disallowed elements etc. Later individual elements can be tested if they are covered by this rule.
Kind: global class
- FixSet
- new FixSet([config])
- .getName(element, [options]) ⇒
string
|undefined
- .has(element) ⇒
boolean
new FixSet([config])
Creates FixSet object. If no include
or exclude
parameters provided or empty configurations are provided, they
would be skipped.
Param | Type | Description |
---|---|---|
[config] | FixSetConfig |
Configuration. |
string
| undefined
fixSet.getName(element, [options]) ⇒ Returns element name if it is covered by rule. Returns undefined otherwise. Prefix and suffix in element name is replaced if requested by rule.
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. |
boolean
fixSet.has(element) ⇒ Returns whether element is covered by rules.
Kind: instance method of FixSet
Returns: boolean
-
Param | Type | Description |
---|---|---|
element | string |
Element name to test. |
Object
FixSetConfig : Fix rule configuration.
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
include | RuleConfig |
Configuration rules for included fields. |
exclude | RuleConfig |
Configuration rules for excluded fields. |
Object
RuleConfig : Fix rule options to create a fix rule from given options. Prefix and suffix parameters can be either string
or regular expression. If they are provided as regular expressions, they must begin with ^
or end with $
.
If no prefixes
and suffixes
provided, it is assumed all strings are included except exceptPrefixes
and exceptSuffixes
.
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. |