-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplify_destination.js
33 lines (29 loc) · 1.09 KB
/
simplify_destination.js
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
import { re } from './helpers.js'
export const connectives = /and|or/gi
export const connective_words = re`\b${connectives}\b`
export const non_connectives = /of|the|for/gi
export const non_connective_words = re`\b${non_connectives}\b`
export const conjunctions = /and|or|of|the|for/gi
export const conjunctive_words = re`\b${conjunctions}\b`
/**
* @param {string} organisation the organisation to check
* @returns {boolean} whether the organisation has an acronym
*/
export function has_acronym(organisation) {
return /\b[A-Z]{2,}\b/.test(organisation)
}
/**
* Returns the acronym of an organisation name
* @param {string} organisation the organisation to convert
* @returns {string} the acronym of the organisation
*/
export function to_acronym(organisation) {
const words = organisation
.trim()
.split(/[^\w]+/g)
.filter(word => !conjunctive_words.test(word))
// if there's less than 3 words or there's an acronym
// we probably shouldn't compress it further
if (words.length < 3 || has_acronym(organisation)) return organisation
return words.map(word => word[0]).join('')
}