Skip to content

Commit

Permalink
Automatic!
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikb committed Jan 10, 2021
1 parent 59f7b6f commit d1da534
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 0 additions & 1 deletion data/pathifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export class Pathifier {
transformer.next = this._transformer;
const lastTransformer = this.lastTransformer();
lastTransformer.next = transformer;
transformer.parent = lastTransformer;
}

map<T = any>(map: Mapper<T>): Pathifier {
Expand Down
15 changes: 10 additions & 5 deletions data/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export class Entries {
}

export interface Transformer {
parent?: Transformer;
entries: Entries;
next?: Transformer;
onValue?: any;
Expand All @@ -94,7 +93,6 @@ export interface Transformer {
}

export abstract class BaseTransformer implements Transformer {
parent?: Transformer;
entries = new Entries();
next?: Transformer;
onOpts?: ListenerCallbackOptions;
Expand Down Expand Up @@ -155,7 +153,7 @@ export class MapTransformer extends BaseTransformer {
}

remove(index: number, entry: Entry): void {
this.entries.remove(entry);
this.entries.remove(entry, index);
this.next?.remove(index, entry);
}

Expand Down Expand Up @@ -333,6 +331,7 @@ export class SliceTransformer extends BaseTransformer {

export class FilterTransformer extends BaseTransformer {
private readonly filter: OnFilter;
private readonly all: Entries = new Entries();

constructor(filter: OnFilter) {
super();
Expand All @@ -341,7 +340,7 @@ export class FilterTransformer extends BaseTransformer {

private _findIndex(key: string): number {
let index = 0;
const entries = this.parent?.entries;
const entries = this.all;
if (entries) {
for (let i = 0; i < entries.length; i++) {
if (entries.get(i).key === key || index >= this.entries.length) {
Expand All @@ -356,6 +355,8 @@ export class FilterTransformer extends BaseTransformer {
}

add(index: number, entry: Entry): void {
this.all.add(entry, index);

if (
this.filter(entry.value, {
opts: entry.opts,
Expand All @@ -370,6 +371,8 @@ export class FilterTransformer extends BaseTransformer {
}

remove(index: number, entry: Entry): void {
this.all.remove(entry, index);

index = this.entries.remove(entry);
if (index >= 0) {
this.next?.remove(index, entry);
Expand All @@ -380,7 +383,7 @@ export class FilterTransformer extends BaseTransformer {
this.onValue = value;
this.onOpts = opts;
let index = 0;
this.parent?.entries.forEach(entry => {
this.all.forEach(entry => {
const test = this.filter(entry.value, {
opts: entry.opts,
onValue: this.onValue,
Expand All @@ -400,6 +403,8 @@ export class FilterTransformer extends BaseTransformer {
}

update(_oldIndex: number, _index: number, entry: Entry): void {
this.all.replace(entry, _index, _oldIndex);

const test = this.filter(entry.value, {
opts: entry.opts,
onValue: this.onValue,
Expand Down
9 changes: 7 additions & 2 deletions domdom/dd-props.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Domode, Mountable } from './types.ts';
import { BaseTransformer, Data } from '../data/index.ts';
import { DomPathifier } from './pathifier.ts';
import { isProbablyPlainObject } from './dom-stower.ts';

function setAttribute(element: HTMLElement, key: string, value: any) {
if (value === null) value = '';
Expand All @@ -10,8 +11,12 @@ function setAttribute(element: HTMLElement, key: string, value: any) {
if (key.startsWith('data-')) {
const dataKey = key.split('-')[1];
element.dataset[dataKey] = value;
} else if (typeof value === 'object') {
Object.assign(element[key], value);
} else if (isProbablyPlainObject(value)) {
if (!element[key]) {
element[key] = value;
} else {
Object.assign(element[key], value);
}
} else {
if (typeof value === 'boolean' || typeof value === 'undefined') {
if (value) {
Expand Down

0 comments on commit d1da534

Please sign in to comment.