untitled notebook

node v10.24.1
version: 1.0.0
endpointsharetweet
const Uniq = require('lodash.uniq'); const StrCommon = (values = []) => { let i = 0; let brk = -1; const strs = values.filter(Boolean); if (!strs.length) { return null; } if (strs.length === 1) { return strs.pop(); } if (Uniq(strs).length === 1) { return strs[0]; } while (brk < 0) { const chars = strs .map(str => str[i]) .map(ch => (ch ? ch.toLowerCase() : ch)) if (chars.every(ch => !ch)) { break; } if (chars.some(ch => ch !== chars[0])) { brk = i; break; } i += 1; } if (brk < 0) { return null; } return strs[0].slice(0, brk); }; console.log(StrCommon([ "Blink-182", "Blink 182", "Blink 182", "Blink 182", "Blink 182", "Blink 182", "Blink 182", "Blink 182", "Blink-182", "Blink-182", "Blink 182", "Blink-182" ])) console.log(StrCommon([ "Enema of the State", "Enema of the State 2CDS", "Enema of the State", "Enema Of The State +9", "Enema of the State", "Enema Of State(Bonus Trck", "Enema Of The State (Vinyl", "Enema Of The State", "Enema of the State", "Enema Of The State", "Enema Of The State (Ltd)", "Enema Of The State (Blue) (Colv)" ]))
Loading…

no comments

    sign in to comment