https://github.com/ozum/fix-set
Lets you define prefix and suffix rules to test strings against.
https://github.com/ozum/fix-set
Last synced: about 2 months ago
JSON representation
Lets you define prefix and suffix rules to test strings against.
- Host: GitHub
- URL: https://github.com/ozum/fix-set
- Owner: ozum
- License: mit
- Created: 2017-03-22T07:36:46.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-04-16T03:11:36.000Z (about 1 year ago)
- Last Synced: 2025-03-10T07:06:23.292Z (2 months ago)
- Language: TypeScript
- Size: 269 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 18
-
Metadata Files:
- Readme: README.hbs
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
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` or `exceptSuffixes` > `prefixes` or `suffixes`
* Prefixes and suffixes has `or` relation. If a string satisfies one of them, rule is satisfied.
* If no `prefixes` and `suffixes` provided, it is assumed all strings are included in rule except `exceptPrefixes` and `exceptSuffixes`.# Synopsis
**TypeScript**
```js
import FixSet, { RuleConfig, FixSetConfig } from 'fix-set';
```**JavaScript**
```js
import FixSet from 'fix-set';
```--------------
```js
// 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
``````js
// 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
``````js
// 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);
``````js
// Usage with lodash.
import lodash from 'lodash';
const filteredObject = lodash.pickBy(data, (value, key) => fixSet.has(key));
``````js
// 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
{{>main~}}