Skip to content

Commit

Permalink
fix: failed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mew-ton committed Jan 25, 2024
1 parent 3dbe9eb commit 7491098
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/fold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function fold<D extends Dictionary>(obj: D, option?: FoldOption): Folded<
...option,
} as FixedFoldOption

const modifier = toModifier(obj, { ...fixedOption, immutable: true })
const modifier = toModifier(obj, fixedOption)

const result = {} as Folded<D>

Expand Down
30 changes: 12 additions & 18 deletions src/lib/modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,27 +196,24 @@ class ObjectModifierImpl<T extends Dictionary> implements ObjectModifier<T> {
}

finalize(): T {
console.log('finalize')
if (this.#opt.immutable) {
return this.#raw
}

const { pruneTargets } = this.#context

for (const target of pruneTargets) {
const targetRef = target.deref()
if (!targetRef) {
for (const targetRef of pruneTargets) {
const target = targetRef.deref()
if (!target) {
continue
}

console.log('targetRef', targetRef)

if (Array.isArray(targetRef)) {
for (let i = targetRef.length - 1; i >= 0; i--) {
if (targetRef[i] !== undefined) {
break
if (Array.isArray(target)) {
for (let i = 0; i < target.length; i++) {
if (target[i] === undefined) {
target.splice(i, 1)
i--
}
targetRef.pop()
}
}
}
Expand Down Expand Up @@ -263,13 +260,6 @@ class ObjectModifierImpl<T extends Dictionary> implements ObjectModifier<T> {
return true
}

const { cacheOriginToModifier } = this.#context

if (!cacheOriginToModifier.has(nextValue)) {
const p = this.#createNext(nextValue, head)
cacheOriginToModifier.set(nextValue, p)
}

const nextModifier = this.#getNextModifier(nextValue, head)

const result = nextModifier.set(rest, newValue)
Expand Down Expand Up @@ -363,6 +353,10 @@ class ObjectModifierImpl<T extends Dictionary> implements ObjectModifier<T> {

cacheOriginToModifier.set(this.#raw, this)
cacheKeyToModifier.set(absoluteKey, new WeakRef(this))

if (!this.#opt.immutable && this.#opt.pruneNilInArray && Array.isArray(this.#raw)) {
this.#context.pruneTargets.add(new WeakRef(this.#raw))
}
}

#findShorthand(absoluteKey: string): { modifier: ObjectModifier; lastKey: string } | undefined {
Expand Down
4 changes: 2 additions & 2 deletions src/twist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export function twist<D extends Dictionary, M extends MoveMap<D>>(
}

const fromKeys = Object.keys(moveMap)
const copyKeys: string[] = []

const src = toModifier(obj, fixedOption)
const dist = createEmptyModifier(fixedOption)
const dist = createEmptyModifier({ ...fixedOption, pruneNilInArray: option?.pruneArray })

const srcKeys = src.keys()
const copyKeys: string[] = []

for (const srcKey of srcKeys) {
const needCopy = fromKeys.every((fromKey) => !startsKeyWith(srcKey, fromKey, fixedOption))
Expand Down

0 comments on commit 7491098

Please sign in to comment.