finished implementation of ArrayFlagSets

This commit is contained in:
2026-03-13 22:07:20 +01:00
parent 8cf5d43e99
commit dda19cdd14
7 changed files with 359 additions and 23 deletions

View File

@@ -1,15 +1,37 @@
import { ArrayFlagSet } from '~'
import { describe, expect, test } from 'vitest'
import { ArrayFlagSet, createArrayFlagSet } from '~'
describe(ArrayFlagSet, () => {
test('none', () => {
const flags = new ArrayFlagSet<string>()
const flags = createArrayFlagSet<string>([])
expect(flags.none()).toEqual([])
})
test('of', () => {
const flags = createArrayFlagSet<string>([])
expect(flags.of()).toEqual([])
expect(flags.of('a')).toEqual(['a'])
expect(flags.of('x', 'y', 'z')).toEqual(['x', 'y', 'z'])
})
test('named', () => {
const flags = createArrayFlagSet([
{ value: 12, as: 'a' },
{ value: 45, as: 'b' },
{ value: 78, as: 'c' },
{ compose: ['a', 'b'], as: 'ab' },
])
expect(flags.named()).toEqual([])
expect(flags.named('a')).toEqual([12])
expect(flags.named('ab', 'c')).toEqual([12, 45, 78])
})
test('union', () => {
const flags = new ArrayFlagSet<string>()
const flags = createArrayFlagSet<string>([])
expect(flags.union([], [])).toEqual([])
expect(flags.union(['A'], [])).toEqual(['A'])
@@ -19,7 +41,7 @@ describe(ArrayFlagSet, () => {
})
test('difference', () => {
const flags = new ArrayFlagSet<string>()
const flags = createArrayFlagSet<string>([])
expect(flags.difference([], [])).toEqual([])
expect(flags.difference(['A'], [])).toEqual(['A'])
@@ -29,7 +51,7 @@ describe(ArrayFlagSet, () => {
})
test('intersection', () => {
const flags = new ArrayFlagSet<string>()
const flags = createArrayFlagSet<string>([])
expect(flags.intersection([], [])).toEqual([])
expect(flags.intersection(['A'], [])).toEqual([])
@@ -40,7 +62,7 @@ describe(ArrayFlagSet, () => {
})
test('isSuperset', () => {
const flags = new ArrayFlagSet<string>()
const flags = createArrayFlagSet<string>([])
expect(flags.isSuperset([], [])).toBe(true)
expect(flags.isSuperset(['A', 'B'], [])).toBe(true)
@@ -50,11 +72,69 @@ describe(ArrayFlagSet, () => {
expect(flags.isSuperset(['C', 'D'], ['B'])).toBe(false)
})
test('hasAny', () => {
const flags = createArrayFlagSet([
{ value: 12, as: 'a' },
{ value: 45, as: 'b', requires: ['a'] },
{ value: 78, as: 'c' },
])
expect(flags.hasAny([], [])).toBe(false)
expect(flags.hasAny([12, 45, 78], [])).toBe(false)
expect(flags.hasAny([12, 45, 78], [12])).toBe(true)
expect(flags.hasAny([12], [12, 78])).toBe(true)
expect(flags.hasAny([45, 78], [45])).toBe(false)
})
test('hasAll', () => {
const flags = createArrayFlagSet([
{ value: 12, as: 'a' },
{ value: 45, as: 'b', requires: ['a'] },
{ value: 78, as: 'c' },
])
expect(flags.hasAll([], [])).toBe(true)
expect(flags.hasAll([12, 45, 78], [])).toBe(true)
expect(flags.hasAll([12, 45, 78], [12])).toBe(true)
expect(flags.hasAll([12], [12, 78])).toBe(false)
expect(flags.hasAll([45, 78], [45])).toBe(false)
})
test('enumerate', () => {
const flags = new ArrayFlagSet<string>()
const flags = createArrayFlagSet<string>([])
expect([...flags.enumerate([])]).toEqual([])
expect([...flags.enumerate(['A'])]).toEqual(['A'])
expect([...flags.enumerate(['A', 'B', 'C'])]).toEqual(['A', 'B', 'C'])
})
test('maximum', () => {
const flags = createArrayFlagSet([
{ value: 12, as: 'a' },
{ value: 45, as: 'b', requires: ['a'] },
{ value: 78, as: 'c', requires: ['b'] },
])
expect(flags.maximum([])).toEqual([])
expect(flags.maximum([12])).toEqual([12])
expect(flags.maximum([45])).toEqual([45, 12])
expect(flags.maximum([78])).toEqual([78, 45, 12])
expect(flags.maximum([99])).toEqual([])
})
test('minimum', () => {
const flags = createArrayFlagSet([
{ value: 12, as: 'a' },
{ value: 45, as: 'b', requires: ['a'] },
{ value: 78, as: 'c', requires: ['b'] },
])
expect(flags.minimum([])).toEqual([])
expect(flags.minimum([12])).toEqual([12])
expect(flags.minimum([45])).toEqual([])
expect(flags.minimum([12, 45])).toEqual([12, 45])
expect(flags.minimum([12, 78])).toEqual([12])
expect(flags.minimum([12, 45, 78])).toEqual([12, 45, 78])
expect(flags.minimum([99])).toEqual([])
})
})