Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stevenvachon/sql-match
Match a string using an SQL pattern.
https://github.com/stevenvachon/sql-match
match nodejs sql wildcard
Last synced: 2 months ago
JSON representation
Match a string using an SQL pattern.
- Host: GitHub
- URL: https://github.com/stevenvachon/sql-match
- Owner: stevenvachon
- License: mit
- Created: 2018-01-08T18:33:33.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-06-01T01:00:57.000Z (over 4 years ago)
- Last Synced: 2024-10-06T04:24:17.000Z (3 months ago)
- Topics: match, nodejs, sql, wildcard
- Language: JavaScript
- Homepage:
- Size: 31.3 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sql-match [![NPM Version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Monitor][greenkeeper-image]][greenkeeper-url]
> Match a string using an SQL pattern.
This library is basically a spec-compliant implementation of a `LIKE` between two strings:
```sql
SELECT 'string' LIKE '%ing'; --> true
```Supported features:
* `%` wildcard sequence
* `_` wildcard
* `\` escapeUnsupported features:
* Custom escape character (`ESCAPE`)
* Ignored trailing spaces (MySQL's `=`)
* Disallowed trailing escape character (PostgreSQL)
* Collated international characters (`COLLATE` with `=`)
* `[charlist]` patterns (Access and SQL Server)
* `?` and `#` wildcards (Access)## Installation
[Node.js](http://nodejs.org/) `>= 8` is required. To install, type this at the command line:
```shell
npm install sql-match
```## Usage
`isSQLMatch(pattern, testString)`
```js
const {isSQLMatch} = require('sql-match');isSQLMatch('string', 'string'); //-> true
isSQLMatch('%ing', 'string'); //-> true
isSQLMatch('s%ng', 'string'); //-> true
isSQLMatch('str%', 'string'); //-> trueisSQLMatch('_tring', 'string'); //-> true
isSQLMatch('st__ng', 'string'); //-> true
isSQLMatch('strin_', 'string'); //-> true
```Optionally, you can create a reusable/cacheable regular expression to improve performance:
```js
const {sqlToRegex} = require('sql-match');const pattern = sqlToRegex('%ing');
['string','stringing'].every(testString => pattern.test(testString));
//-> true
```## Gotchas
Because JavaScript strings are interpreted, you may want to use [`String.raw`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/raw) to avoid some annoyances that reduce consistency with SQL.
Non-wildcard escape sequences are possible:
```js
isSQLMatch('\t', ' '); //-> true
isSQLMatch('\u0020', ' '); //-> true
// or
isSQLMatch(String.raw`\t`, 't'); //-> true
isSQLMatch(String.raw`\u0020`, 'u0020'); //-> true
```
```sql
SELECT 't' LIKE '\t'; --> true
SELECT 'u0020' LIKE '\u0020'; --> true
```Matching a literal wildcard will require you to escape the escape character:
```js
isSQLMatch('\\%trin\\_', '%trin_'); //-> true
// or
isSQLMatch(String.raw`\%trin\_`, '%trin_'); //-> true
```
```sql
SELECT '%trin_' LIKE '\%trin\_'; --> true
```Matching a literal backslash will require you to escape the escaped escape character:
```js
isSQLMatch('\\\\string', '\\string'); //-> true
// or
isSQLMatch(String.raw`\\string`, String.raw`\string`); //-> true
```
```sql
SELECT '\string' LIKE '\\string'; --> true
```[npm-image]: https://img.shields.io/npm/v/sql-match.svg
[npm-url]: https://npmjs.com/package/sql-match
[travis-image]: https://img.shields.io/travis/stevenvachon/sql-match.svg
[travis-url]: https://travis-ci.org/stevenvachon/sql-match
[coveralls-image]: https://img.shields.io/coveralls/stevenvachon/sql-match.svg
[coveralls-url]: https://coveralls.io/github/stevenvachon/sql-match
[greenkeeper-image]: https://badges.greenkeeper.io/stevenvachon/sql-match.svg
[greenkeeper-url]: https://greenkeeper.io/