An open API service indexing awesome lists of open source software.

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.

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~}}