0.3.4 • Published 6 years ago

fix-set v0.3.4

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

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

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

new FixSet(config)

ParamTypeDescription
configFixSetConfigConfiguration.

fixSet.getName(element, options) ⇒ string | undefined

Kind: instance method of FixSet
Returns: string | undefined -

ParamTypeDefaultDescription
elementstringElement name to test whether it is covered by rule.
optionsObject{}Options
options.replacePrefixboolean | undefinedWhether it should prefix be stripped from start of field name. Defaults to value given during object cunstruction.
options.replaceSuffixboolean | undefinedWhether 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 -

ParamTypeDescription
elementstringElement name to test.

FixSetConfig : Object

Kind: global typedef
Properties

NameTypeDescription
includeRuleConfigConfiguration rules for included fields.
excludeRuleConfigConfiguration rules for excluded fields.

RuleConfig : Object

Kind: global typedef
Properties

NameTypeDescription
elementsstring | Array.<string> | Set.<string>Strings which are covered by rule. They are compared by equal operator.
exceptstring | Array.<string> | Set.<string>Fields which are not covered by rule.
prefixesstring | RegExp | Array.<(string|RegExp)> | Set.<(string|RegExp)>Strings which starts with given prefixes are covered by rule.
suffixesstring | RegExp | Array.<(string|RegExp)> | Set.<(string|RegExp)>Strings which ends with given suffixes are covered by rule.
exceptPrefixesstring | RegExp | Array.<(string|RegExp)> | Set.<(string|RegExp)>Strings which starts with given prefixes are NOT covered by rule.
exceptSuffixesstring | RegExp | Array.<(string|RegExp)> | Set.<(string|RegExp)>Strings which ends with given suffixes are NOT covered by rule.
replacePrefixbooleanWhether it should prefix be stripped from start of field name
replaceSuffixbooleanWhether it should suffix be stripped from end of field name.
0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago