-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new util findNearestPrefixFromArray, update available method.
- Loading branch information
Showing
5 changed files
with
97 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import isPrefixName from "./is-prefix-name"; | ||
|
||
export default function findNearestPrefixFromArray<T extends string>(name: string, arrayPrefixes: T[]): T | undefined { | ||
let answer: T | undefined; | ||
|
||
arrayPrefixes.forEach(prefix => { | ||
|
||
if (!isPrefixName(name, prefix)) return; | ||
if (prefix.length > (answer?.length || 0)) answer = prefix; | ||
}) | ||
|
||
return answer?.length ? answer : undefined | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import findNearestPrefixFromArray from "../../../src/utils/find-nearest-prefix-from-array"; | ||
|
||
describe("Nearest name", () => { | ||
|
||
test("Name is founded.", () => { | ||
expect(findNearestPrefixFromArray('address.city', ['city', 'address', 'address.city'])).toBe('address') | ||
}) | ||
|
||
test("Name not founded", () => { | ||
expect(findNearestPrefixFromArray('address.city', ['a', 'b', 'city', 'addre'])).toBe(undefined) | ||
}) | ||
|
||
test("Founded more nearest", () => { | ||
expect(findNearestPrefixFromArray('address.city.name', ['address', 'city', 'address.city'])).toBe('address.city'); | ||
}) | ||
test("Founded more nearest with other order", () => { | ||
expect(findNearestPrefixFromArray('address.city.name', ['address.city', 'address', 'city'])).toBe('address.city'); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters