1 import { matchEvery } from './match-every';
3 describe('matchEvery', () => {
4 describe('All needles match', () => {
5 test('should return `true` if all needles are in the haystack', () => {
6 const needles = ['title', 'username', 'my secure notes', 'example.com'];
7 const matcher = matchEvery(needles);
8 expect(matcher('title username my secure notes example.com')).toBe(true);
9 expect(matcher('example.com my secure notes username title')).toBe(true);
12 test('should return `true` if needles partially match the haystack', () => {
13 const needles = ['titl', 'user', 'secure', 'example'];
14 const matcher = matchEvery(needles);
15 expect(matcher('title username my secure notes example.com')).toBe(true);
18 test('should return `true` if needles match in any order', () => {
19 const needles = ['title', 'username', 'my secure notes', 'example.com'];
20 const matcher = matchEvery(needles);
21 expect(matcher('example.com title my secure notes username')).toBe(true);
22 expect(matcher('username my secure notes example.com title')).toBe(true);
25 test('should return `true` if case-insensitive', () => {
26 const needles = ['title', 'username', 'my secure notes', 'example.com'];
27 const matcher = matchEvery(needles);
28 expect(matcher('Title Username My Secure Notes Example.com')).toBe(true);
32 describe('Partial and No Matches', () => {
33 test('should return `false` if any needle is missing from the haystack', () => {
34 const needles = ['title', 'username', 'my secure notes', 'example.com'];
35 const matcher = matchEvery(needles);
36 expect(matcher('title username my secure notes')).toBe(false);
37 expect(matcher('example.com my secure notes title')).toBe(false);
38 expect(matcher('title username example.com')).toBe(false);
41 test('should return `false` if no needles match', () => {
42 const needles = ['title', 'username', 'my secure notes', 'example.com'];
43 const matcher = matchEvery(needles);
44 expect(matcher('nothing')).toBe(false);
45 expect(matcher('titles')).toBe(false);
48 test('should return `false` if any partial needle does not match', () => {
49 const needles = ['titl', 'user', 'security', 'examp'];
50 const matcher = matchEvery(needles);
51 expect(matcher('title username my secure notes example')).toBe(false);
55 describe('Special Cases', () => {
56 test('should return `true` if needles match with extra whitespace', () => {
57 const needles = ['title', 'username', 'my', 'secure-notes', 'example.com'];
58 const matcher = matchEvery(needles);
59 expect(matcher('title username my secure-notes example.com')).toBe(true);
62 test('should return `true` for needles with special characters', () => {
63 const needles = ['title!', 'user@name', 'my$secure^notes', 'example.com#'];
64 const matcher = matchEvery(needles);
65 expect(matcher('title! user@name my$secure^notes example.com#')).toBe(true);
68 test('should return `true` if newline-separated needles are all present', () => {
69 const needles = ['title', 'username', 'my secure notes', 'example.com'];
70 const matcher = matchEvery(needles);
71 expect(matcher('title\nusername\nmy secure notes\nexample.com')).toBe(true);
74 test('should return `true` if extra whitespace in haystack', () => {
75 const needles = ['title', 'username', 'my secure notes', 'example.com'];
76 const matcher = matchEvery(needles);
77 expect(matcher(' title username my secure notes example.com ')).toBe(true);
80 test('should return `false` if any newline-separated needle does not match', () => {
81 const needles = ['title', 'username', 'my secure notes', 'example.com'];
82 const matcher = matchEvery(needles);
83 expect(matcher('title\nusername\nmy secure notes')).toBe(false);
86 test('should return `false` if special characters do not match', () => {
87 const needles = ['title!', 'user@name', 'my$secure^notes', 'example.com#'];
88 const matcher = matchEvery(needles);
89 expect(matcher('title user name my secure notes example.com')).toBe(false);
92 test('should return `false` for non-normalized needles', () => {
93 const needles = ['Title', 'Username', 'My Secure Notes', 'Example.com'];
94 const matcher = matchEvery(needles);
95 expect(matcher('title username my secure notes example.com')).toBe(false);
98 test('should return `false` if empty needles', () => {
99 const needles: string[] = [];
100 const matcher = matchEvery(needles);
101 expect(matcher('title username my secure notes example.com')).toBe(false);
104 test('should return `false` for empty haystack with empty needles', () => {
105 const needles: string[] = [];
106 const matcher = matchEvery(needles);
107 expect(matcher('')).toBe(false);
110 test('should return `false` for empty haystack with non-empty needles', () => {
111 const needles = ['title', 'username', 'my', 'secure-notes', 'example.com'];
112 const matcher = matchEvery(needles);
113 expect(matcher('')).toBe(false);