@technobuddha/library
Version:
A large library of useful functions
31 lines (30 loc) • 1.05 kB
JavaScript
import clean from '../clean';
/**
* Convert a string into a sortable string
*
* @remarks for example "The Beatles" becomes "Beatles, The"
* @param input string to convert
* @param __namedParameters see {@link Options}
* @return sortable string
*/
export function sortOrder(input, { ignoreQuotes = true, moveArticles = true } = {}) {
input = clean(input);
if (ignoreQuotes && input.startsWith('"')) {
const quote = input.slice(0, 1);
input = input.slice(1);
const index = input.indexOf(quote, 1);
if (index >= 0)
input = input.slice(0, index) + input.slice(index + 1);
}
const lc = input.toLowerCase();
if (moveArticles) {
if (lc.startsWith('a '))
input = `${input.slice(2)}, ${input.slice(0, 1)}`;
else if (lc.startsWith('an '))
input = `${input.slice(3)}, ${input.slice(0, 2)}`;
else if (lc.startsWith('the '))
input = `${input.slice(4)}, ${input.slice(0, 3)}`;
}
return input;
}
export default sortOrder;