From 1a337a261a6dd6deffa836cbd949bb036e103f36 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 10 May 2015 08:59:14 +0100 Subject: Search completion; reuse previous query. --- lib/utils.coffee | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lib/utils.coffee') diff --git a/lib/utils.coffee b/lib/utils.coffee index 16adc305..a1ed23c2 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -184,6 +184,16 @@ Utils = return true if re.test string false + # Calculate the length of the longest shared prefix of a list of strings. + longestCommonPrefix: (strings) -> + return 0 unless 0 < strings.length + strings.sort (a,b) -> a.length - b.length + [ shortest, strings... ] = strings + for ch, index in shortest.split "" + for str in strings + return index if ch != str[index] + return shortest.length + # Convenience wrapper for setTimeout (with the arguments around the other way). setTimeout: (ms, func) -> setTimeout func, ms -- cgit v1.2.3