项目作者: gajus

项目描述 :
Destructive foreign ID normalization.
高级语言: JavaScript
项目地址: git://github.com/gajus/normalize-id.git
创建时间: 2020-02-07T03:49:48Z
项目社区:https://github.com/gajus/normalize-id

开源协议:Other

下载


normalize-id

Travis build status
Coveralls
NPM version
Canonical Code Style
Twitter Follow

Destructive foreign ID normalization.

Use case

To normalize unstable inputs that are treated like IDs into a stable ID form.

This is useful when you do not trust the foreign ID representation to be stable, e.g. when you are treating URLs as IDs of a foreign resource and those URLs can have varying representation. However, it also means that two foreign IDs that vary in their semantic meaning only in characters that are removed or transliterated during normalization will produce the same ID, e.g. deja and déjà are both normalized to DEJA.

Although arbitrary, the applied normalization rules are derived through trial-and-error by aggregating data from thousands of different websites and optimizing for the most stable outputs with the least collisions.

The normalized IDs conform to ^(?!_)[A-Z0-9_]+(?<!_)$.

A non-exhaustive example of inputs and outputs (in each paragraph: first line is input, second – output):

  1. fooBarBaz
  2. FOO_BAR_BAZ
  3. FooBarBaz
  4. FOO_BAR_BAZ
  5. foo-bar-baz
  6. FOO_BAR_BAZ
  7. foo---bar---baz
  8. FOO_BAR_BAZ
  9. foo___bar___baz
  10. FOO_BAR_BAZ
  11. foo:::bar:::baz
  12. FOO_BAR_BAZ
  13. foo bar baz
  14. FOO_BAR_BAZ

Inputs can also be URLs, e.g.

  1. # Protocol is excluded. URL query parameter semantics is recognized.
  2. http://foo.com/bar/baz-baz.baz?qux=quux#corge
  3. FOO_COM_BAR_BAZ_BAZ_BAZ_QUX_QUUX_CORGE
  4. # URL query parameters are sorted.
  5. http://foo.tld/?bar=baz&qux=quux
  6. FOO_TLD_BAR_BAZ_QUX_QUUX

Refer to the test cases to view the normalization rules.

API

  1. import {
  2. normalizeId,
  3. } from 'normalize-id';
  4. normalizeId(foreignId: string): string;