npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fix-set

v0.3.4

Published

Lets you define prefix and suffix rules to test strings against.

Downloads

9

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

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])

| Param | Type | Description | | --- | --- | --- | | [config] | FixSetConfig | Configuration. |

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

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

fixSet.has(element) ⇒ boolean

Kind: instance method of FixSet
Returns: boolean -

| Param | Type | Description | | --- | --- | --- | | element | string | Element name to test. |

FixSetConfig : Object

Kind: global typedef
Properties

| Name | Type | Description | | --- | --- | --- | | include | RuleConfig | Configuration rules for included fields. | | exclude | RuleConfig | Configuration rules for excluded fields. |

RuleConfig : Object

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