项目作者: JosephusPaye

项目描述 :
Match a piece of text to common formats like URLs, email addresses, colors, and more.
高级语言: JavaScript
项目地址: git://github.com/JosephusPaye/match-common-formats.git
创建时间: 2020-12-20T06:42:56Z
项目社区:https://github.com/JosephusPaye/match-common-formats

开源协议:MIT License

下载


match-common-formats

Match a piece of text to common formats like URLs, email addresses, colors, and more.

Due to the large number of formats to match, this project is completed in parts:

  • Part 1: URIs (URIs + URNs + URLs) and IP Addresses (IPv4 + IPv6): Published in v0.1.0.
  • Part 2: Colors - RGB hexadecimal (8, 6, 4, and 3-character codes); rgb(), rgba(), hsl(), and hsla() (with comma and space separators): Published in v0.2.0.
  • Part 3: Email addresses and social tokens (@mentions and #hashtags): Published in v0.3.0.
  • Part 4: Currency (e.g. 2,000AUD or LRD2,000,000.75): Published in v0.4.0.

This project is part of #CreateWeekly, my attempt to create something new publicly every week in 2020.

Demo

Installation

  1. npm install -g @josephuspaye/match-common-formats

Usage

Match with all matchers

The following example shows how to match a piece of text with all matchers:

  1. import { match } from '@josephuspaye/match-common-formats';
  2. const first = match('example.com'); // matches a URL
  3. const second = match('192.168.0.1'); // matches an IP address
  4. const third = match('hello@example.com'); // matches an email address and a URL
  5. console.log({ first, second, third });

View output

json { "first": [ { "type": "url", "label": "Web URL", "input": "example.com", "url": "http://example.com", "scheme": "http" } ], "second": [ { "type": "ip-address", "label": "IPv4 Address", "input": "192.168.0.1", "version": "ipv4", "address": "192.168.0.1" } ], "third": [ { "type": "email-address", "label": "Email Address", "input": "hello@example.com", "address": "hello@example.com" }, { "type": "url", "label": "Web URL", "input": "hello@example.com", "url": "http://hello@example.com", "scheme": "http" } ] }

Stop matching after first match

The following example shows how to match a piece of text with all matchers, stopping after the first match:

  1. import { match } from '@josephuspaye/match-common-formats';
  2. // Would normally match a hex color code and a hashtag, but specifying `matchOne`
  3. // stops after the first match, returning only the color match
  4. const matches = match('#bad', { matchOne: true });
  5. console.log({ matches });

View output

json { "matches": [ { "input": "#bad", "expected": [ { "type": "color", "label": "Hexadecimal Color Code", "input": "#bad", "format": "hex", "color": "#bad" } ] } ] }

Match with select matchers

The following example shows how to match a piece of text with select matchers:

  1. import {
  2. match,
  3. matchUri,
  4. matchIpAddress,
  5. } from '@josephuspaye/match-common-formats';
  6. const first = match('example.com', { matchers: [matchUri] });
  7. const second = match('example.com', { matchers: [matchIpAddress] }); // matches will be empty
  8. console.log({ first, second });

View output

json { "first": [ { "type": "url", "label": "Web URL", "input": "example.com", "url": "http://example.com", "scheme": "http" } ], "second": [] }

Match with a specific matcher

The following example shows how to match a piece of text with a specific matcher:

  1. import { match, matchIpAddress } from '@josephuspaye/match-common-formats';
  2. const first = matchIpAddress('10.0.0.1');
  3. const second = matchIpAddress('example.com'); // no match
  4. console.log({ first, second });

View output

json { "first": { "type": "ip-address", "label": "IPv4 Address", "input": "10.0.0.1", "version": "ipv4", "address": "10.0.0.1" }, "second": null }

Available formats

Format Type Input example
URI uri (data) data:image/png;base64,...
URN urn (isbn) urn:isbn:0451450523
URL url (http) get.pizza
URL url (https) https://example.com
URL url (http) localhost:3000
IP Address ip-address (ipv4) 127.0.0.1
IP Address ip-address (ipv6) ::1
RGB Color (Hex) color (hex) #bad, #bada55, #abcd, #aabbccdd
RGB Color color (rgb) rgb(22, 22, 22)
RGB Alpha Color color (rgba) rgba(22, 22, 22, 0.5)
HSL Color color (hsl) hsl(22deg, 50%, 20%)
HSL Alpha Color color (hsla) hsla(22deg, 50%, 20%, 80%)
Email address* email-address john.doe+newsletters@example.com
Social token (mention) mention @JosephusPaye
Social token (hashtag) hashtag #CreateWeekly
Currency currency 2,000,000LRD
Currency currency AUD2000.00

Notes

  • The email address matcher is not technically “complete”, as it doesn’t match addresses with unknown TLDs, IP addresses, or special characters in the local part that are not +, -, _, or .. It is designed to match only a subset of technically valid email address, ones that are more “common”.

API

  1. interface MatchCommon {
  2. type: string;
  3. label: string;
  4. input: string;
  5. }
  6. interface Url extends MatchCommon {
  7. type: 'url';
  8. url: string;
  9. scheme: string;
  10. }
  11. interface Uri extends MatchCommon {
  12. type: 'uri';
  13. uri: string;
  14. scheme: string;
  15. }
  16. interface Urn extends MatchCommon {
  17. type: 'urn';
  18. urn: string;
  19. namespaceId: string;
  20. namespaceString: string;
  21. }
  22. interface IpAddress extends MatchCommon {
  23. type: 'ip-address';
  24. version: 'ipv4' | 'ipv6';
  25. address: string;
  26. }
  27. interface Color extends MatchCommon {
  28. type: 'color';
  29. format: 'hex' | 'rgb' | 'rgba' | 'hsl' | 'hsla';
  30. color: string;
  31. }
  32. interface EmailAddress extends MatchCommon {
  33. type: 'email-address';
  34. address: string;
  35. }
  36. interface SocialToken extends MatchCommon {
  37. type: 'mention' | 'hashtag';
  38. /**
  39. * The mention or hashtag, with the @ or # prefix
  40. */
  41. token: string;
  42. }
  43. interface KnownCurrency {
  44. code: string;
  45. name: string;
  46. symbol: string;
  47. otherSymbols?: string[];
  48. }
  49. interface Currency extends MatchCommon {
  50. type: 'currency';
  51. currency: KnownCurrency;
  52. amount: number;
  53. }
  54. type Match =
  55. | Uri
  56. | Url
  57. | Urn
  58. | IpAddress
  59. | Color
  60. | EmailAddress
  61. | SocialToken
  62. | Currency;
  63. type Matcher = (string: string) => Match | null;
  64. interface MatchOptions {
  65. /**
  66. * A list of matchers to apply, defaults to all matchers
  67. */
  68. matchers?: Matcher[];
  69. /**
  70. * Stop matching after the first match
  71. * @default false
  72. */
  73. matchOne?: boolean;
  74. }
  75. /**
  76. * The default matchers
  77. */
  78. const defaultMatchers: Matcher[];
  79. /**
  80. * Match the given string to a URN, URL, or URI
  81. */
  82. function matchUri(string: string): Url | Uri | Urn | null;
  83. /**
  84. * Match the given string to an IPv4 or IPv6 address
  85. */
  86. function matchIpAddress(string: string): IpAddress | null;
  87. /**
  88. * Match the given string to a hex (RGB), rgb(), rgba(), hsl(), or hsla() color code
  89. */
  90. function matchColor(string: string): Color | null;
  91. /**
  92. * Match the given string to an email address
  93. */
  94. function matchEmailAddress(string: string): EmailAddress | null;
  95. /**
  96. * Match the given string to a social token (@mention or #hashtag)
  97. */
  98. function matchSocialToken(string: string): SocialToken | null;
  99. /**
  100. * Match the given string to a currency value
  101. */
  102. function matchCurrency(
  103. string: string,
  104. options?: {
  105. matchSymbol?: boolean;
  106. decimalSymbol?: ',' | '.';
  107. thousandSeparator?: ',' | '.' | ' ';
  108. }
  109. ): Currency | null;
  110. /**
  111. * Compare the given string to known formats, optionally only those
  112. * matched by the given matchers, and get the matches
  113. */
  114. function match(string: string, options?: MatchOptions): Match[];

Licence

MIT