Coder Social home page Coder Social logo

pocesar / js-diacritic-regex Goto Github PK

View Code? Open in Web Editor NEW
26.0 4.0 9.0 96 KB

Creates the inverse of transliterated string to a regex. What? Basically, diacritic insensitiveness

License: MIT License

JavaScript 47.24% TypeScript 52.76%
regex database diacritics transliteration transliterate regexp regexp-search regex-match accent accents

js-diacritic-regex's Introduction

Build Status Coverage Status

NPM

Diacritic regex

Creates the inverse of transliterated string to a regex. What? Basically, a regex that is diacritic insensitive

Why?

Sometimes the user will search for blasé, but your database is dumb and doesn't understand collations and diacritic insensitiveness, but it can compare stuff using regex, so there ya go.

How?

Suppose you have the word résumé but written improperly in the database as resume. The user is clever, and types it correctly into the search box. Gets nothing. How to search for all the weird cases people mistype stuff when comes to accents?

import { toRegex, toString } from 'diacritic-regex';

toRegex()('résumé') // => /r[eEÉéÈèÊêëË]s[úùÚÙüÜuU]m[eEÉéÈèÊêëË]/i;
toRegex({flags: 'mu'})('résumé') // => /r[eEÉéÈèÊêëË]s[úùÚÙüÜuU]m[eEÉéÈèÊêëË]/mu;
toRegex({
  flags: '',
  mappings: {
    'e': 'eéÉ'
  }
})('résumé') // => /r[eéÉ]s[úùÚÙüÜuU]m[eéÉ]/;

toString({
    mappings: {
        '*': ['\\S+'] // literals, won't try to wrap in []'s,
        'u': ['u']
    }
})('résumé*') // => 'r[eEÉéÈèÊêëË]sum[eEÉéÈèÊêëË]\S+'

If you want to change the mappings for all instances:

import { mappings } from 'diacritic-regex'

mappings['*'] = ['[\\S\\s]+']

Caveats

Be aware of RegExp.prototype.exec with g flag being stateful

The i flag is appended to the RegExp flags if you don't pass any flags to toRegex

Compatibility

Work in node and the browser, but needs polyfills for Array.reduce, Array.map and Object.keys depending on how old your target browser is

License

MIT

js-diacritic-regex's People

Contributors

dependabot[bot] avatar pocesar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

js-diacritic-regex's Issues

Unexpected token

I'm getting this warning message and can't figure out what causing it

WARNING in ./~/diacritic-regex/index.d.ts
Module parse failed: my_project/node_modules/diacritic-regex/index.d.ts Unexpected token (1:7)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (1:7)
    at Parser.pp$4.raise (my_project/node_modules/acorn/dist/acorn.js:2221:15)
    at Parser.pp.unexpected (my_project/node_modules/acorn/dist/acorn.js:603:10)
    at Parser.pp.expect (my_project/node_modules/acorn/dist/acorn.js:597:28)
    at Parser.pp$1.parseExportSpecifiers (my_project/node_modules/acorn/dist/acorn.js:1215:10)
    at Parser.pp$1.parseExport (my_project/node_modules/acorn/dist/acorn.js:1186:30)
    at Parser.pp$1.parseStatement (my_project/node_modules/acorn/dist/acorn.js:719:71)
    at Parser.pp$1.parseTopLevel (my_project/node_modules/acorn/dist/acorn.js:638:25)
    at Parser.parse (my_project/node_modules/acorn/dist/acorn.js:516:17)
    at Object.parse (my_project/node_modules/acorn/dist/acorn.js:3098:39)
    at Parser.parse (my_project/node_modules/webpack/lib/Parser.js:902:15)
 @ ./~/diacritic-regex ^\.\/.*$

Returning uppercase and lowercase variations with /i

When regex option "i" is passed, it's not necessary to generate upper and lowercase variations in regex.

Example:
toRegex({ flags: 'i' })('olá') should return /ol[aáàãä]/i;

When flag 'i' is not passed, should return /ol[aAáÁàÀãÃäÄ]/

Issue with special characters (, ), \,

calling toRegex with a string containing open parenthesis (, ) or backslash \, return
Uncaught SyntaxError: Invalid regular expression: //: \ at end of pattern

For example, calling toRegex({ flags: 'i' })("résumé ("); throw

Uncaught SyntaxError: Invalid regular expression: /r[EeÈèÉéÊêËë]s[UuÙùÚúÛûÜü]m[EeÈèÉéÊêËë] (/: Unterminated group

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.