fix-set
TypeScript icon, indicating that this package has built-in type declarations

0.3.4 • Public • Published

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

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 no prefixes and suffixes provided, it is assumed all strings are included except exceptPrefixes and exceptSuffixes.

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

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.

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

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 -

  • Element name if it is covered by rule, undefined otherwise. Name getName prefix and suffix replaced if requested by rule.
  • 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

    Returns whether element is covered by rules.

    Kind: instance method of FixSet
    Returns: boolean -

    • Whether element is covered by rule.
    • Param Type Description
      element string

      Element name to test.

      FixSetConfig : Object

      Fix rule configuration.

      Kind: global typedef
      Properties

      Name Type Description
      include RuleConfig

      Configuration rules for included fields.

      exclude RuleConfig

      Configuration rules for excluded fields.

      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 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.

      Readme

      Keywords

      Package Sidebar

      Install

      npm i fix-set

      Weekly Downloads

      12

      Version

      0.3.4

      License

      MIT

      Unpacked Size

      49.2 kB

      Total Files

      23

      Last publish

      Collaborators

      • ozum