Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traits manual apply #31

Merged
merged 48 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
578d568
callable works as a trait
BenZed Feb 16, 2023
3f7855e
sbtract publicstructural away from public struct
BenZed Feb 19, 2023
2667f41
cleanup
BenZed Feb 19, 2023
445fddf
pipes now support expressions
BenZed Feb 21, 2023
36678c6
added word/lines compose methods in string
BenZed Feb 21, 2023
5bbce1a
prepped for refactor
BenZed Feb 21, 2023
82fc90f
node trait/tests implemented
BenZed Feb 22, 2023
50aef93
node MOSTLY implemented
BenZed Feb 22, 2023
d1a54e3
node moved to it's own package
BenZed Feb 22, 2023
6395e07
node table implemented
BenZed Feb 22, 2023
73436c5
everything plays nice with callable
BenZed Feb 22, 2023
d95bfa8
node 95% complete
BenZed Feb 22, 2023
fb2dcc5
Merge branch 'is-refactor-validator-traits-analyze' into composite-tr…
BenZed Feb 22, 2023
2921449
callable as a trait
BenZed Feb 23, 2023
6cbd15a
util reorganized
BenZed Feb 23, 2023
f014403
immutable/node changes to take new callable functionality into account
BenZed Feb 23, 2023
cc76158
Merge pull request #29 from BenZed/composite-traits-callable-as-trait
BenZed Feb 23, 2023
ecf7013
minor mutator tests
BenZed Feb 23, 2023
059b00d
NodeTableBuilder now uses a mutator
BenZed Feb 23, 2023
cc83072
Merge pull request #30 from BenZed/composite-traits-callable-as-trait
BenZed Feb 23, 2023
9eea906
node folder reorganized
BenZed Feb 23, 2023
df797e1
validation contet is now nodified
BenZed Feb 24, 2023
f5e5378
context in
BenZed Feb 24, 2023
2a29746
okay, validate base classes are sorted
BenZed Feb 24, 2023
3faee70
completed validator and traits
BenZed Feb 24, 2023
2e58f98
tests written for the validator traits
BenZed Feb 25, 2023
3efe98b
default and cast tests written
BenZed Feb 25, 2023
ef36f14
traits are great, but I'm not going to use them just for the sake of …
BenZed Feb 25, 2023
802e954
porting over prevoius validators
BenZed Feb 25, 2023
603b3c1
reorganized validaotrs
BenZed Feb 25, 2023
2d3c355
re-added instance validator
BenZed Feb 25, 2023
913d150
added tuple validator
BenZed Feb 25, 2023
92faa2c
analyze now just receives a context
BenZed Feb 25, 2023
d2fc6ce
validation error/context sorted
BenZed Feb 25, 2023
fabe02e
all tests pass
BenZed Feb 25, 2023
975eb67
shape validator in
BenZed Feb 25, 2023
7cec978
modifiers back in
BenZed Feb 26, 2023
2ce9346
schema is in
BenZed Feb 26, 2023
5aee90c
mutator trait application changed
BenZed Feb 26, 2023
7d0756b
all tests pass
BenZed Feb 26, 2023
156f73d
schema in
BenZed Feb 26, 2023
62373f5
auto parent clear on property delete
BenZed Feb 27, 2023
913eee2
union validator added
BenZed Feb 27, 2023
fa57e57
added clearSubContexts method
BenZed Feb 27, 2023
79c2dd7
switching to manually applied traits
BenZed Feb 27, 2023
4a0419a
traits are now applied manually
BenZed Feb 27, 2023
3624838
Structural copy implementation now attempts to use Copy.super impleme…
BenZed Feb 28, 2023
4001ab2
manually applied traits to validation
BenZed Feb 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module.exports = {
'max-len': ['error', 150],
'nonblock-statement-body-position': ['warn', 'below'],
'no-return-assign': 'error',
'@typescript-eslint/ban-types': ['error', {
types: {
"{}": false,
"Function": false
}
}],
'@typescript-eslint/brace-style': ['error'],
'@typescript-eslint/prefer-optional-chain': ['warn'],
'@typescript-eslint/prefer-nullish-coalescing': ['warn'],
Expand Down
1 change: 1 addition & 0 deletions packages/immutable/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './traits'

export * from './structs'
62 changes: 41 additions & 21 deletions packages/immutable/src/structs/array-struct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { RecordStruct } from './record-struct'

import { test, expect } from '@jest/globals'
import { expectTypeOf } from 'expect-type'
import { equals, Stateful, StructState, Structural } from '../traits'

import {
equals,
PublicStructural,
Stateful,
StructState,
Structural
} from '../traits'

//// Setup ////

Expand All @@ -13,6 +20,10 @@ const array = new ArrayStruct(...data)

//// Tests ////

test('public structural methods', () => {
expect(PublicStructural.is(array)).toBe(true)
})

test('get State', () => {

const state = Stateful.get(array)
Expand All @@ -26,51 +37,60 @@ test('get State', () => {
})

expect(state).toEqual({ ...array })

expect(array.get()).toEqual(state)
})

test('apply', () => {
test('create', () => {

const array2 = Structural.apply(array, [5,4,3,2,1,0])
const array2 = Structural.create(array, [5,4,3,2,1,0])

expect(Structural.getIn(array2)).toEqual({
expect(Structural.get(array2)).toEqual({
'0': 5,
'1': 4,
'2': 3,
'3': 2,
'4': 1,
'5': 0,
})

expect(array.create([5,4,3,2,1,0])).toEqual(array2)
})

test('deep apply', () => {
test('deep create', () => {

const array2 = Structural.apply(array, 0, 100)
const array2 = Structural.create(array, 0, 100)

expect(Structural.getIn(array2)).toEqual({
expect(Structural.get(array2)).toEqual({
0: 100,
1: 2,
2: 3,
3: 4,
4: 5
})

expect(array2.create(0, 100)).toEqual(array2)
})

test('deep nested apply', () => {
test('deep nested create', () => {

const array = new ArrayStruct(

new RecordStruct({
foo: 0,
bar: 'yes'
}),

new RecordStruct({
foo: 'bar',
bar: 10
})

)

const array2 = Structural.apply(array, 0, 'bar', 100)
const array2 = Structural.create(array, 0, 'bar', 100)

expect(Structural.getIn(array2)).toEqual({
expect(Structural.get(array2)).toEqual({
0: {
bar: 100,
foo: 0
Expand All @@ -81,6 +101,16 @@ test('deep nested apply', () => {
}
})

expect(array.create(0, 'bar', 100)).toEqual(array2)

})

test('copy', () => {
expect(array.copy()).toEqual(array)
})

test('equals', () => {
expect(array.copy().equals(array)).toBe(true)
})

test('state type', () => {
Expand Down Expand Up @@ -202,17 +232,7 @@ describe('array interface', () => {

test('filter', () => {
const arrayFiltered = array.filter(i => i < 2)
expect(Stateful.get(arrayFiltered)).toEqual({
0: 1
})
expect(array).not.toBe(arrayFiltered)
})

test('filter', () => {
const arrayFiltered = array.filter(i => i < 2)
expect(Stateful.get(arrayFiltered)).toEqual({
0: 1
})
expect(Stateful.get(arrayFiltered)).toEqual({ 0: 1 })
expect(array).not.toBe(arrayFiltered)
})

Expand Down
17 changes: 8 additions & 9 deletions packages/immutable/src/structs/array-struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { assign, each, Func, Mutable, nil, omit } from '@benzed/util'

import { adjacent, shuffle } from '@benzed/array'
import { Traits } from '@benzed/traits'
import { Stateful, Structural } from '../traits'

import { PublicStructural, Stateful, Structural } from '../traits'

//// EsLint ////

Expand All @@ -18,8 +19,6 @@ type ArrayMethodNames = keyof {

type ArrayParams<T, M extends ArrayMethodNames> = Parameters<Array<T>[M]>

// type ArrayReturns<T, M extends ArrayMethodNames> = ReturnType<Array<T>[M]>

//// Helper ////

const ArrayMethods = Array.prototype
Expand Down Expand Up @@ -47,7 +46,7 @@ function applyArrayState<

// clone struct and apply state
const newArrayStruct = Object.create(arrayStruct.constructor.prototype)
Structural.setIn(newArrayStruct, state)
Structural.set(newArrayStruct, state)
return newArrayStruct

}
Expand All @@ -58,7 +57,7 @@ function applyArrayState<
* An ArrayStruct implements a subset of the Array's methods, with the caveat that
* none of the methods mutate the original array.
*/
class ArrayStruct<T> extends Traits.use(Structural) implements Iterable<T> {
class ArrayStruct<T> extends Traits.use(PublicStructural) implements Iterable<T> {

readonly [index: number]: T

Expand All @@ -73,7 +72,7 @@ class ArrayStruct<T> extends Traits.use(Structural) implements Iterable<T> {
//// Interface ////

get length(): number {
return each.nameOf(this).count
return each.nameOf(this).count()
}

at(index: number): T | nil {
Expand Down Expand Up @@ -202,7 +201,7 @@ class ArrayStruct<T> extends Traits.use(Structural) implements Iterable<T> {
// TODO indexOf
// TODO keys
// TODO lastIndexOf
// TODO getIn
// TODO get
// TODO applyIn
// TODO copy
// TODO equals
Expand All @@ -221,11 +220,11 @@ class ArrayStruct<T> extends Traits.use(Structural) implements Iterable<T> {

//// State ////

get [Stateful.key](): { [index: number]: T } {
get [Stateful.state](): { [index: number]: T } {
return { ...this }
}

set [Stateful.key](state: { [index: number]: T }) {
set [Stateful.state](state: { [index: number]: T }) {
assign(this, state)
}

Expand Down
6 changes: 3 additions & 3 deletions packages/immutable/src/structs/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

export * from './data-struct'

export * from './public-struct'

export * from './record-struct'
export * from './record-struct'

export * from './array-struct'
19 changes: 7 additions & 12 deletions packages/immutable/src/structs/public-struct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ const vector = new class Vector extends PublicStruct {

const edge = new class Edge extends PublicStruct {
readonly a = vector
readonly b = vector
readonly b = vector
}

test('get', () => {
const vstate = Structural.getIn(vector)
const vstate = Structural.get(vector)
expect(vstate).toEqual(vector.get())

const estate = Structural.getIn(edge)
const estate = Structural.get(edge)
expect(estate).toEqual(edge.get())
})

Expand All @@ -34,29 +34,24 @@ test('get deep', () => {

test('apply', () => {

const vector2 = vector.apply({ x: 100 })
const vector2 = vector.create({ x: 100 })
expect(vector2.get()).toEqual({ x: 100, y: 0 })

const edge2 = edge.apply({ a: vector2 })
const edge2 = edge.create({ a: vector2 })
expect(edge2.get()).toEqual({ a: { x: 100, y: 0 }, b: { x: 0, y: 0 }})

})

test('apply deep', () => {

const edge3 = edge.apply('a', 'x', 50)

const edge3 = edge.create('a', 'x', 50)
expect(edge3.get()).toEqual({ a: { x: 50, y: 0 }, b: { x: 0, y: 0 }})
})

test('copy', () => {

expect(edge.copy()).not.toBe(edge)
expect(edge.copy()).toEqual(edge)

})

test('equals', () => {
expect(edge.equals(edge.copy())).toBe(true)
expect(edge.equals(edge.apply('a', 'x', 50 ))).not.toBe(true)
expect(edge.equals(edge.create('a', 'x', 50 ))).not.toBe(true)
})
53 changes: 18 additions & 35 deletions packages/immutable/src/structs/public-struct.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,47 @@
import { Traits } from '@benzed/traits'
import { assign, NamesOf, omit } from '@benzed/util'
import { copy, equals, StructState, StructStateApply, StructStatePath, Structural } from '../traits'

import { PublicStructural, Structural } from '../traits'

//// EsLint ////

/* eslint-disable
@typescript-eslint/no-explicit-any,
/* eslint-disable
@typescript-eslint/no-explicit-any
*/

//// PublicStruct ////

/**
* State preset for a generic objects.
* Any property is considered state, so long as it isn't an object prototype property.
* State preset for a public structural objects.
* Any property is considered state, as long as it isn't a immutable interface method, or
* a built-in object method.
*/
export type PublicStructState<T extends object> =
Pick<
T,
Exclude<NamesOf<T>, 'toString' | 'valueOf' | 'copy' | 'equals' | 'get' | 'apply'>
>

//// PublicStruct ////

/**
* A public struct is a structural object with a public interface for immutable operations
* It assumes that any non-public-interface key is state.
* A PublicStruct implements the PublicStructural interface, and treats
* everything except public immutability methods (and toString, valueOf)
* as state.
*/
export abstract class PublicStruct extends Traits.use(Structural) {
export abstract class PublicStruct extends Traits.use(PublicStructural) {

//// Public Immutability Interface ////

get<P extends StructStatePath>(...path: P): StructState<this, P> {
return Structural.getIn(this, ...path)
}

apply<P extends StructStatePath>(...pathAndState: [...path: P, state: StructStateApply<this, P>]): this {
return Structural.apply(this, ...pathAndState)
}

copy(): this {
return copy(this)
}

equals(other: unknown): other is this {
return equals(this, other)
}

//// Structural Implemenation ////

get [Structural.key](): PublicStructState<this> {
get [Structural.state](): PublicStructState<this> {
return omit(
this,
'toString',
this,
'toString',
'valueOf',
'get',
'apply',
'copy',
'equals'
) as PublicStructState<this>
}

protected set [Structural.key](state: PublicStructState<this>) {
protected set [Structural.state](state: PublicStructState<this>) {
assign(this, state)
}

Expand Down
Loading