-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ts
74 lines (68 loc) · 1.77 KB
/
utils.ts
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
import { Pruner } from "./constants"
const defaultPruner: Pruner<any> = () => true;
/**
* Get all prefixes of a given string in descending order of length.
*
* @example
* for (const s of utils.decreasingPrefixesOf("abc")) {
* console.log(s); // prints "abc", "ab", "a"
* }
*
* @example
* for (const s of utils.decreasingPrefixesOf("")) { } // loop never entered
*/
function* decreasingPrefixesOf(s: string): IterableIterator<string> {
for (let i = s.length; i > 0; i--) {
yield s.slice(0, i);
}
};
/**
* Get all prefixes of a given string in ascending order of length.
*
* @example
* for (const s of utils.increasingPrefixesOf("abc")) {
* console.log(s); // prints "a", "ab", "abc"
* }
*
* @example
* for (const s of utils.increasingPrefixesOf("")) { } // never enters loops
*/
function* increasingPrefixesOf(s: string): IterableIterator<string> {
const slength = s.length;
for (let i = 1; i <= slength; i++) {
yield s.slice(0, i);
}
};
/**
* Get the longest shared prefix of two strings.
*
* @param {string} s1 - the first string
* @param {string} s2 - the second string
*
* @returns {string} - the longest shared prefix of s1 and s2, undefined if no shared prefix
*
* @example
* utils.longestSharedPrefix("abc", "ab"); // "ab"
*
* @example
* utils.longestSharedPrefix("abc", "xx"); // undefined
*
* @example
* utils.longestSharedPrefix("abc", "")
*/
function longestSharedPrefix(s1: string, s2: string): string|undefined {
const maxLen = Math.max(s1.length, s2.length);
for (var i = 0; i < maxLen; i++)
if (s1[i] != s2[i])
break;
return i > 0 ? s1.slice(0, i) : undefined;
};
/**
* Some utility functions and type definitions.
*/
export {
decreasingPrefixesOf,
increasingPrefixesOf,
longestSharedPrefix,
defaultPruner,
};