Skip to content

Latest commit

 

History

History
396 lines (272 loc) · 8.81 KB

api.value.filter.md

File metadata and controls

396 lines (272 loc) · 8.81 KB

Filter Attribute Value Modules

@mcaskill/html-build-attributes/value/index.js

The filter modules are a collection of functions and function factories to approve, reject, and mutate a value.

For all API functions listed:

  • T represents any data type.
  • AttrName is a string and represents an attribute name.
  • AttrValue is a string or boolean and represents a filtered attribute value.
  • AttrValueFilter is a function signature representing the filter.

AttrValueFilter

This type represents the signature of a filter function.

Syntax

(value: T, name?: AttrName, fallback?: AttrValueFilter | AttrValue) => AttrValue

Example

function filterNumber(value, name, fallback = false) {
  if (typeof value === 'number') {
    return value;
  }

  return fallback;
}

For a custom filter to play nice as middleware, it is recommended to use the filterFallback() function to resolve any fallbacks that might be a function:

import {
    filterFallback,
} from '@mcaskill/html-build-attributes/value/filter-fallback.js';

function filterNumber(value, name, fallback = false) {
  if (typeof value === 'number') {
    return value;
  }

  return filterFallback(value, name, fallback);
}

createFilterCallable()

@mcaskill/html-build-attributes/value/filter-callable.js

Creates a filter function that applies the given filter to a given value or it's returned result if the value is a function.

Syntax

createFilterCallable(
  filter: AttrValueFilter
): AttrValueFilter

Examples

import {
  createFilterCallable
} from '@mcaskill/html-build-attributes/value/filter-function.js';

const filterNumber = (value, name, fallback = false) => {
  if (typeof value === 'number') {
    return value;
  }

  return fallback;
};

const filterValueOrClosure = createFilterCallable(
  filterNumber
);

filterValueOrClosure(42);
// → 42

filterValueOrClosure(() => (42 * 2));
// → 84

filterValueOrClosure('hello');
// → false

filterValueOrClosure(() => 'hello');
// → false

createFilterList()

@mcaskill/html-build-attributes/value/filter-list.js

Creates a filter function that applies a filter to each item of the array and concatenates the list into a string separated by a customizable delimiter.

Syntax

createFilterList(
  filter: AttrValueFilter,
  oneOrManySeparators: string | object<AttrName, string>,
  fallbackSeparator?: string
): AttrValueFilter

Examples

import {
  createFilterList
} from '@mcaskill/html-build-attributes/value/filter-list.js';

const filterTokenList = createFilterList(filterToken, {
  'coords': ',',
}, ':');

filterTokenList([ 'a', 'b', 'c' ]);
// → a:b:c

filterTokenList([ 'a', 'b', 'c' ], 'coords');
// → a,b,c

const scopedFilterTokenList = createFilterList(filterToken, {
  'class': ' ',
});

scopedFilterTokenList([ 'a', 'b', 'c' ], 'class');
// → a b c

scopedFilterTokenList([ 'a', 'b', 'c' ]);
// → TypeError<array separator is not defined>

scopedFilterTokenList([ 'a', 'b', 'c' ], 'rel');
// → TypeError<attribute [rel] separator is not defined>

createFilterMiddleware()

@mcaskill/html-build-attributes/value/filter-middleware.js

Creates a filter function from a collection of filters that returns the filtered value from the first filter that returns a value.

Syntax

createFilterMiddleware(
  filters: AttrValueFilter[]
): AttrValueFilter

Examples

import {
  createFilterMiddleware
} from '@mcaskill/html-build-attributes/value/filter-middleware.js';

const filterString = (value, name, next) => {
  if (typeof value === 'string') {
    return value;
  }

  return next(value, name);
};

const filterNumber = (value, name, next) => {
  if (typeof value === 'number') {
    return value;
  }

  return next(value, name);
};

const filterByType = createFilterMiddleware([
  filterString,
  filterNumber,
]);

filterByType('hello');
// → 'hello'

filterByType(42);
// → 42

filterByType([]);
// → false

filterByType(true);
// → false

filterByType(true, 'test', 'fallback');
// → 'fallback'

filterFallback()

@mcaskill/html-build-attributes/value/filter-fallback.js

This function resolves the fallback of an attribute value filter.

If the fallback argument is a function it is assumed to be another filter and is called with the value and name arguments, otherwise it is assumed to be the value to return.

Syntax

filterFallback(
  value: T,
  name?: AttrName,
  fallback?: AttrValueFilter | AttrValue
): AttrValue

Examples

import {
  filterFallback
} from '@mcaskill/html-build-attributes/value/filter-fallback.js';

function filterNumber(value, name, fallback = false) {
  if (typeof value === 'number') {
    return value;
  }

  return filterFallback(value, name, fallback);
}

filterNumber('hello', 'value');
// → false

filterNumber('hello', 'value', 0);
// → 0

filterNumber('hello', 'value', (value, name) => Number.parseInt(value));
// → NaN

filterStringable()

@mcaskill/html-build-attributes/value/filter-stringable.js

This function converts a value to a string representation or to a JSON string.

Syntax

filterStringable(value: T, name?: AttrName): AttrValue

Description

If the value is not nullish (null or undefined) and:

  1. a Date, the attribute is rendered and the value is converted to ISO format.

  2. stringable, the attribute is rendered and the value is converted to its string representation.

  3. Otherwise, the attribute is rendered and the value is serialized as a JSON string.

Examples

import {
  filterStringable
} from '@mcaskill/html-build-attributes/value/filter-stringable.js';

filterStringable(new Date(2006, 0, 2, 15, 4, 5));
// → 2006-01-02T20:04:05.000Z

class Person {
  constructor(id, name) {
    this.id = id;
    this.name = name;
  }
}

filterStringable(new Person(1, 'Tim'));
// → {"id":1,"name":"Tim"}

class JsonablePerson extends Person {
  toJSON() {
    return `${this.id}:${this.name}`;
  }
}

filterStringable(new JsonablePerson(1, 'Tim'));
// → "1:Tim"

class StringablePerson extends Person {
  toString() {
    return this.name;
  }
}

filterStringable(new StringablePerson(1, 'Tim'));
// → Tim

filterToken()

@mcaskill/html-build-attributes/value/filter-token.js

This function filters a value that is a string, number, boolean, or BigInt.

This filter is designed to replicate the behaviour of most token list attributes such as accept, class, coords, rel, sizes, and srcset.

Syntax

filterToken(value: T, name?: AttrName): AttrValue

Description

If the value is:

  1. a string, the value is concatenated.

    Leading and trailing whitespace is preserved.

    An empty string is rendered.

  2. a boolean, the value is converted to a string and is concatenated.

  3. a BigInt, the value is concatenated.

  4. a number and finite, the value is concatenated.

    A sign of -0 is preserved.

filterValue()

@mcaskill/html-build-attributes/value/filter-value.js

This function filters a value that is a string, number, boolean, or BigInt.

This filter is designed to replicate the behaviour of most attributes including boolean attributes and WAI-ARIA attributes.

Syntax

filterValue(value: T, name?: AttrName): AttrValue

Description

If the value is:

  1. a boolean and

    1. the attribute name starts with aria-, the boolean is converted to a string.

    2. is true, only the attribute name is rendered.

    3. is false, the attribute is discarded.

  2. a string, the attribute is rendered.

    Leading and trailing whitespace is preserved.

    An empty string is rendered.

  3. a BigInt, the attribute is rendered.

  4. a number and finite, the attribute is rendered.

    A sign of -0 is preserved.