Remove isSimilar
This commit is contained in:
@ -1,25 +0,0 @@
|
|||||||
---
|
|
||||||
title: isSimilar
|
|
||||||
tags: string,intermediate
|
|
||||||
---
|
|
||||||
|
|
||||||
Determines if the `pattern` matches with `str`.
|
|
||||||
|
|
||||||
Use `String.toLowerCase()` to convert both strings to lowercase, then loop through `str` and determine if it contains all characters of `pattern` and in the correct order.
|
|
||||||
Adapted from [here](https://github.com/forrestthewoods/lib_fts/blob/80f3f8c52db53428247e741b9efe2cde9667050c/code/fts_fuzzy_match.js#L18).
|
|
||||||
|
|
||||||
```js
|
|
||||||
const isSimilar = (pattern, str) =>
|
|
||||||
[...str].reduce(
|
|
||||||
(matchIndex, char) =>
|
|
||||||
char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase()
|
|
||||||
? matchIndex + 1
|
|
||||||
: matchIndex,
|
|
||||||
0
|
|
||||||
) === pattern.length;
|
|
||||||
```
|
|
||||||
|
|
||||||
```js
|
|
||||||
isSimilar('rt','Rohit'); // true
|
|
||||||
isSimilar('tr','Rohit'); // false
|
|
||||||
```
|
|
||||||
@ -1,11 +0,0 @@
|
|||||||
const {isSimilar} = require('./_30s.js');
|
|
||||||
|
|
||||||
test('isSimilar is a Function', () => {
|
|
||||||
expect(isSimilar).toBeInstanceOf(Function);
|
|
||||||
});
|
|
||||||
test('isSimilar returns true', () => {
|
|
||||||
expect(isSimilar('rt', 'Rohit')).toBeTruthy();
|
|
||||||
});
|
|
||||||
test('isSimilar returns false', () => {
|
|
||||||
expect(isSimilar('tr', 'Rohit')).toBeFalsy();
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user