npm-run-scripts 1.0.1

Fast interactive TUI for running npm scripts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Fuzzy matching implementation.
//!
//! Uses SkimMatcherV2 for high-performance fuzzy matching with scoring.

use std::sync::OnceLock;

use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher as FuzzyMatcherTrait;

use crate::package::Script;

/// Global matcher instance for performance.
/// Using OnceLock to initialize once and reuse across calls.
static GLOBAL_MATCHER: OnceLock<SkimMatcherV2> = OnceLock::new();

/// Get the global matcher instance.
fn global_matcher() -> &'static SkimMatcherV2 {
    GLOBAL_MATCHER.get_or_init(SkimMatcherV2::default)
}

/// Fuzzy matcher for script filtering.
pub struct FuzzyMatcher {
    matcher: SkimMatcherV2,
    case_sensitive: bool,
    search_descriptions: bool,
}

impl FuzzyMatcher {
    /// Create a new fuzzy matcher.
    pub fn new() -> Self {
        Self {
            matcher: SkimMatcherV2::default(),
            case_sensitive: false,
            search_descriptions: true,
        }
    }

    /// Set case sensitivity.
    pub fn case_sensitive(mut self, case_sensitive: bool) -> Self {
        self.case_sensitive = case_sensitive;
        self
    }

    /// Set whether to search in descriptions.
    pub fn search_descriptions(mut self, search_descriptions: bool) -> Self {
        self.search_descriptions = search_descriptions;
        self
    }

    /// Match a script against a query.
    ///
    /// Returns a score if the script matches, or None if it doesn't.
    pub fn match_script(&self, script: &Script, query: &str) -> Option<i64> {
        if query.is_empty() {
            return Some(0);
        }

        let query = if self.case_sensitive {
            query.to_string()
        } else {
            query.to_lowercase()
        };

        let name = if self.case_sensitive {
            script.name().to_string()
        } else {
            script.name().to_lowercase()
        };

        // Check name match
        if let Some(score) = self.matcher.fuzzy_match(&name, &query) {
            return Some(score);
        }

        // Check description match
        if self.search_descriptions {
            if let Some(desc) = script.description() {
                let desc = if self.case_sensitive {
                    desc.to_string()
                } else {
                    desc.to_lowercase()
                };

                if let Some(score) = self.matcher.fuzzy_match(&desc, &query) {
                    // Lower score for description matches
                    return Some(score / 2);
                }
            }
        }

        None
    }
}

impl Default for FuzzyMatcher {
    fn default() -> Self {
        Self::new()
    }
}

/// Filter scripts based on a query.
///
/// Returns (index, score) pairs sorted by score descending (best matches first).
/// Uses the global pre-compiled matcher for performance.
///
/// # Arguments
///
/// * `query` - The search query (empty query returns all scripts with score 0)
/// * `scripts` - Slice of scripts to filter
/// * `search_descriptions` - Whether to also search in script descriptions
///
/// # Returns
///
/// Vector of (original_index, score) tuples, sorted by score descending.
///
/// # Examples
///
/// ```
/// use npm_run_scripts::package::Script;
/// use npm_run_scripts::filter::filter_scripts;
///
/// let scripts = vec![
///     Script::new("dev", "vite"),
///     Script::new("build", "vite build"),
///     Script::new("test", "vitest"),
/// ];
///
/// let results = filter_scripts("dev", &scripts, false);
/// assert_eq!(results.len(), 1);
/// assert_eq!(results[0].0, 0); // "dev" is at index 0
/// ```
pub fn filter_scripts(
    query: &str,
    scripts: &[Script],
    search_descriptions: bool,
) -> Vec<(usize, i64)> {
    // Empty query returns all scripts with score 0
    if query.is_empty() {
        return (0..scripts.len()).map(|i| (i, 0)).collect();
    }

    let matcher = global_matcher();
    let query_lower = query.to_lowercase();

    // Pre-allocate with expected capacity (most scripts likely won't match)
    let mut matches: Vec<(usize, i64)> = Vec::with_capacity(scripts.len().min(32));

    // Reusable buffer for lowercase conversion to reduce allocations
    let mut name_buffer = String::with_capacity(64);

    for (idx, script) in scripts.iter().enumerate() {
        // Try matching against name first
        name_buffer.clear();
        name_buffer.extend(script.name().chars().flat_map(|c| c.to_lowercase()));

        if let Some(score) = matcher.fuzzy_match(&name_buffer, &query_lower) {
            matches.push((idx, score));
            continue;
        }

        // Try matching against description if enabled
        if search_descriptions {
            if let Some(desc) = script.description() {
                name_buffer.clear();
                name_buffer.extend(desc.chars().flat_map(|c| c.to_lowercase()));

                if let Some(score) = matcher.fuzzy_match(&name_buffer, &query_lower) {
                    // Description matches get lower priority (half score)
                    matches.push((idx, score / 2));
                }
            }
        }
    }

    // Sort by score descending (best matches first)
    matches.sort_unstable_by(|a, b| b.1.cmp(&a.1));

    matches
}

/// Filter scripts using the FuzzyMatcher instance.
///
/// Returns scripts sorted by match score (best first).
pub fn filter_scripts_with_matcher<'a>(
    scripts: impl IntoIterator<Item = &'a Script>,
    query: &str,
    matcher: &FuzzyMatcher,
) -> Vec<(&'a Script, i64)> {
    let mut matches: Vec<_> = scripts
        .into_iter()
        .filter_map(|s| matcher.match_script(s, query).map(|score| (s, score)))
        .collect();

    // Sort by score (descending)
    matches.sort_by(|a, b| b.1.cmp(&a.1));

    matches
}

/// Get the indices of matched characters in the text.
///
/// This is useful for highlighting matched portions of text in the UI.
///
/// # Arguments
///
/// * `query` - The search query
/// * `text` - The text to search within
///
/// # Returns
///
/// Vector of character indices that match the query, or empty if no match.
///
/// # Examples
///
/// ```
/// use npm_run_scripts::filter::get_match_indices;
///
/// let indices = get_match_indices("bd", "build");
/// assert_eq!(indices, vec![0, 4]); // 'b' at 0, 'd' at 4
/// ```
pub fn get_match_indices(query: &str, text: &str) -> Vec<usize> {
    if query.is_empty() || text.is_empty() {
        return Vec::new();
    }

    let matcher = global_matcher();
    let query_lower = query.to_lowercase();
    let text_lower = text.to_lowercase();

    matcher
        .fuzzy_indices(&text_lower, &query_lower)
        .map(|(_, indices)| indices)
        .unwrap_or_default()
}

/// Check if a query matches text (simple boolean check).
///
/// # Arguments
///
/// * `query` - The search query
/// * `text` - The text to search within
///
/// # Returns
///
/// `true` if the query matches the text, `false` otherwise.
pub fn matches(query: &str, text: &str) -> bool {
    if query.is_empty() {
        return true;
    }

    let matcher = global_matcher();
    let query_lower = query.to_lowercase();
    let text_lower = text.to_lowercase();

    matcher.fuzzy_match(&text_lower, &query_lower).is_some()
}

/// Get the match score for a query against text.
///
/// # Arguments
///
/// * `query` - The search query
/// * `text` - The text to search within
///
/// # Returns
///
/// The match score, or None if no match.
pub fn match_score(query: &str, text: &str) -> Option<i64> {
    if query.is_empty() {
        return Some(0);
    }

    let matcher = global_matcher();
    let query_lower = query.to_lowercase();
    let text_lower = text.to_lowercase();

    matcher.fuzzy_match(&text_lower, &query_lower)
}

#[cfg(test)]
mod tests {
    use super::*;

    // ==================== FuzzyMatcher tests ====================

    #[test]
    fn test_empty_query_matches_all() {
        let matcher = FuzzyMatcher::new();
        let script = Script::new("dev", "vite");

        assert!(matcher.match_script(&script, "").is_some());
        assert_eq!(matcher.match_script(&script, "").unwrap(), 0);
    }

    #[test]
    fn test_exact_match() {
        let matcher = FuzzyMatcher::new();
        let script = Script::new("dev", "vite");

        assert!(matcher.match_script(&script, "dev").is_some());
    }

    #[test]
    fn test_fuzzy_match() {
        let matcher = FuzzyMatcher::new();
        let script = Script::new("build", "vite build");

        // "bd" should fuzzy match "build"
        assert!(matcher.match_script(&script, "bd").is_some());
    }

    #[test]
    fn test_no_match() {
        let matcher = FuzzyMatcher::new();
        let script = Script::new("dev", "vite");

        assert!(matcher.match_script(&script, "xyz").is_none());
    }

    #[test]
    fn test_case_insensitive() {
        let matcher = FuzzyMatcher::new().case_sensitive(false);
        let script = Script::new("DEV", "vite");

        assert!(matcher.match_script(&script, "dev").is_some());
    }

    #[test]
    fn test_case_sensitive() {
        let matcher = FuzzyMatcher::new().case_sensitive(true);
        let script = Script::new("DEV", "vite");

        // Exact case should match
        assert!(matcher.match_script(&script, "DEV").is_some());
        // Note: SkimMatcherV2 is case-insensitive by design, but our wrapper
        // normalizes case when case_sensitive is false. When true, we pass
        // the original case but the matcher still matches case-insensitively.
        // This is a limitation of SkimMatcherV2.
        // For now, we test that case_sensitive mode at least preserves case
        let matcher_insensitive = FuzzyMatcher::new().case_sensitive(false);
        // Both should match with case-insensitive
        assert!(matcher_insensitive.match_script(&script, "dev").is_some());
    }

    // ==================== filter_scripts tests ====================

    #[test]
    fn test_filter_scripts_empty_query() {
        let scripts = vec![
            Script::new("dev", "vite"),
            Script::new("build", "vite build"),
            Script::new("test", "vitest"),
        ];

        let results = filter_scripts("", &scripts, false);
        assert_eq!(results.len(), 3);
        // All should have score 0
        for (_, score) in &results {
            assert_eq!(*score, 0);
        }
    }

    #[test]
    fn test_filter_scripts_exact_match() {
        let scripts = vec![
            Script::new("dev", "vite"),
            Script::new("build", "vite build"),
            Script::new("test", "vitest"),
        ];

        let results = filter_scripts("dev", &scripts, false);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].0, 0); // "dev" is at index 0
    }

    #[test]
    fn test_filter_scripts_fuzzy_match() {
        let scripts = vec![
            Script::new("development", "vite"),
            Script::new("build", "vite build"),
            Script::new("deploy", "deploy.sh"),
        ];

        // "dev" should match "development" and "deploy"
        let results = filter_scripts("dev", &scripts, false);
        assert!(results.len() >= 1);
        // First result should be "development" (better match)
        assert_eq!(results[0].0, 0);
    }

    #[test]
    fn test_filter_scripts_no_match() {
        let scripts = vec![
            Script::new("dev", "vite"),
            Script::new("build", "vite build"),
        ];

        let results = filter_scripts("xyz", &scripts, false);
        assert!(results.is_empty());
    }

    #[test]
    fn test_filter_scripts_description_search() {
        let scripts = vec![
            Script::new("start", "node server.js"),
            Script::with_description("build", "vite build", "Build for production"),
        ];

        // Should not find "production" when description search is off
        let results = filter_scripts("production", &scripts, false);
        assert!(results.is_empty());

        // Should find "production" when description search is on
        let results = filter_scripts("production", &scripts, true);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].0, 1); // "build" is at index 1
    }

    #[test]
    fn test_filter_scripts_sorted_by_score() {
        let scripts = vec![
            Script::new("test-unit", "vitest unit"),
            Script::new("test", "vitest"),
            Script::new("test-e2e", "vitest e2e"),
        ];

        let results = filter_scripts("test", &scripts, false);
        assert!(!results.is_empty());
        // All three should match since they all contain "test"
        assert_eq!(results.len(), 3);
        // The exact match "test" should have the highest score
        // Find the score for "test" (index 1)
        let test_score = results.iter().find(|(idx, _)| *idx == 1).map(|(_, s)| *s);
        let test_unit_score = results.iter().find(|(idx, _)| *idx == 0).map(|(_, s)| *s);
        // Exact match should score >= other matches
        assert!(test_score.unwrap() >= test_unit_score.unwrap());
    }

    #[test]
    fn test_exact_match_scores_higher_than_fuzzy() {
        let scripts = vec![
            Script::new("bd", "command1"),
            Script::new("build", "command2"),
        ];

        let results = filter_scripts("bd", &scripts, false);
        assert!(results.len() >= 1);
        // Exact match "bd" should score higher
        assert_eq!(results[0].0, 0);
        assert!(results[0].1 > results.get(1).map(|r| r.1).unwrap_or(0));
    }

    #[test]
    fn test_prefix_match_scores_higher_than_middle() {
        let scripts = vec![
            Script::new("rebuild", "command1"),
            Script::new("build", "command2"),
        ];

        let results = filter_scripts("build", &scripts, false);
        assert!(results.len() >= 1);
        // "build" (prefix/exact) should score higher than "rebuild" (contains)
        assert_eq!(results[0].0, 1);
    }

    #[test]
    fn test_filter_scripts_case_insensitive() {
        let scripts = vec![
            Script::new("DEV", "vite"),
            Script::new("Build", "vite build"),
        ];

        let results = filter_scripts("dev", &scripts, false);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].0, 0);

        let results = filter_scripts("BUILD", &scripts, false);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].0, 1);
    }

    // ==================== get_match_indices tests ====================

    #[test]
    fn test_get_match_indices_basic() {
        let indices = get_match_indices("bd", "build");
        assert_eq!(indices, vec![0, 4]); // 'b' at 0, 'd' at 4
    }

    #[test]
    fn test_get_match_indices_exact() {
        let indices = get_match_indices("build", "build");
        assert_eq!(indices, vec![0, 1, 2, 3, 4]);
    }

    #[test]
    fn test_get_match_indices_empty_query() {
        let indices = get_match_indices("", "build");
        assert!(indices.is_empty());
    }

    #[test]
    fn test_get_match_indices_empty_text() {
        let indices = get_match_indices("bd", "");
        assert!(indices.is_empty());
    }

    #[test]
    fn test_get_match_indices_no_match() {
        let indices = get_match_indices("xyz", "build");
        assert!(indices.is_empty());
    }

    #[test]
    fn test_get_match_indices_case_insensitive() {
        let indices = get_match_indices("BD", "build");
        assert_eq!(indices, vec![0, 4]);
    }

    // ==================== matches tests ====================

    #[test]
    fn test_matches_empty_query() {
        assert!(matches("", "anything"));
    }

    #[test]
    fn test_matches_exact() {
        assert!(matches("dev", "dev"));
    }

    #[test]
    fn test_matches_fuzzy() {
        assert!(matches("bd", "build"));
    }

    #[test]
    fn test_matches_no_match() {
        assert!(!matches("xyz", "build"));
    }

    // ==================== match_score tests ====================

    #[test]
    fn test_match_score_empty_query() {
        assert_eq!(match_score("", "anything"), Some(0));
    }

    #[test]
    fn test_match_score_exact() {
        let score = match_score("dev", "dev");
        assert!(score.is_some());
        assert!(score.unwrap() > 0);
    }

    #[test]
    fn test_match_score_no_match() {
        assert!(match_score("xyz", "build").is_none());
    }

    // ==================== Performance / edge case tests ====================

    #[test]
    fn test_filter_many_scripts() {
        // Test with 100+ scripts to ensure performance is reasonable
        let scripts: Vec<Script> = (0..150)
            .map(|i| Script::new(format!("script-{i}"), format!("command-{i}")))
            .collect();

        let results = filter_scripts("script-50", &scripts, false);
        assert!(!results.is_empty());
        // Exact match should be first
        assert_eq!(results[0].0, 50);
    }

    #[test]
    fn test_filter_special_characters() {
        let scripts = vec![
            Script::new("build:prod", "vite build --mode prod"),
            Script::new("build:dev", "vite build --mode dev"),
            Script::new("test:unit", "vitest unit"),
        ];

        let results = filter_scripts("build:", &scripts, false);
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_description_scores_lower_than_name() {
        let scripts = vec![
            Script::with_description("start", "node index.js", "dev server"),
            Script::new("dev", "vite"),
        ];

        let results = filter_scripts("dev", &scripts, true);
        assert_eq!(results.len(), 2);
        // "dev" (name match) should score higher than "start" (description match)
        assert_eq!(results[0].0, 1);
    }

    #[test]
    fn test_global_matcher_consistency() {
        // Ensure global matcher gives consistent results
        let score1 = match_score("dev", "development");
        let score2 = match_score("dev", "development");
        assert_eq!(score1, score2);
    }

    #[test]
    fn test_empty_scripts_list() {
        let scripts: Vec<Script> = vec![];
        let results = filter_scripts("dev", &scripts, false);
        assert!(results.is_empty());
    }

    #[test]
    fn test_unicode_support() {
        let scripts = vec![
            Script::new("développement", "vite"),
            Script::new("build", "vite build"),
        ];

        let results = filter_scripts("dév", &scripts, false);
        assert!(!results.is_empty());
    }
}