Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
just a simple reduce with some domain, pre-refactoring
import {List, Map} from 'immutable'
test('test name goes here', () => {
const currentFlags = List([
Map({id: 7, tag: 'flag/bbb:JAR'}),
Map({id: 6, tag: 'flag/bbb'}),
Map({id: 9, tag: 'flag/ccc:ADV'}),
Map({id: 8, tag: 'flag/bbb:ELI'}),
Map({id: 10, tag: 'flag/ccc:JAR'})
])
const expectedResults = Map([
[List([7, 6, 8]), 'bbb'],
[List([9, 10]), 'ccc'],
])
expect(availableFlagsOptions(currentFlags)).toStrictEqual(Map(expectedResults))
})
export function availableFlagsOptions (currentFlags) {
let availableFlags = Map()
if (currentFlags) {
const reversedMultiIdMapping = currentFlags.reduce((accumulator, flag) => {
const flagBaseName = getFlagBaseName(flag)
let ids = accumulator.get(flagBaseName) || List()
ids = ids.push(flag.get('id'))
let result = accumulator.set(flagBaseName, ids)
return result
}, Map())
availableFlags = reversedMultiIdMapping.mapEntries(([k, v]) => [v, k])
}
return availableFlags
}
const FLAG_PREFIX = 'flag/'
const PENDING_FLAG_PREFIX = `${FLAG_PREFIX}pending/`
export function getFullFlagName (flag) {
return flag.get('tag')
}
export function getFlagName (flag) {
const flagName = getFullFlagName(flag)
if (flagName.startsWith(PENDING_FLAG_PREFIX)) {
return flagName.slice(PENDING_FLAG_PREFIX.length)
} else if (flagName.startsWith(FLAG_PREFIX)) {
return flagName.slice(FLAG_PREFIX.length)
}
return flagName
}
export function getFlagBaseName (flag) {
const flagName = getFlagName(flag)
return flagName.split(':')[0]
}
export function flagIsPending (flag) {
return getFullFlagName(flag).startsWith(PENDING_FLAG_PREFIX)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.