• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atinc / ngx-tethys / d9ae709b-3c27-4b69-b125-b8b80b54f90b

pending completion
d9ae709b-3c27-4b69-b125-b8b80b54f90b

Pull #2757

circleci

mengshuicmq
fix: fix code review
Pull Request #2757: feat(color-picker): color-picker support disabled (#INFR-8645)

98 of 6315 branches covered (1.55%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

2392 of 13661 relevant lines covered (17.51%)

83.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/util/immutable.ts
1
import { Id } from 'ngx-tethys/types';
2
import { coerceArray, isFunction, isUndefinedOrNull } from './helpers';
3

4
export interface EntityAddOptions {
×
5
    prepend?: boolean;
×
6

×
7
    afterId?: Id;
×
8
}
9

10
export interface EntityMoveOptions {
11
    afterId?: Id;
12

13
    toIndex?: number;
14
}
15

16
export interface ProducerOptions {
17
    idKey?: string;
18
}
19

20
export class Producer<TEntity> {
×
21
    private idKey = '_id';
×
22

×
23
    private entities: TEntity[];
24

×
25
    constructor(entities: TEntity[], options?: ProducerOptions) {
×
26
        this.entities = entities || [];
×
27
        if (options && options.idKey) {
×
28
            this.idKey = options.idKey;
×
29
        }
30
    }
×
31

×
32
    /**
33
     * Add an entity or entities.
×
34
     *
×
35
     * @example
36
     * produce([users]).add(Entity);
37
     * produce([users]).add([Entity, Entity]);
38
     * produce([users]).add(Entity, { prepend: true });
×
39
     * produce([users]).add(Entity, { afterId: '' });
40
     */
×
41
    add(entity: TEntity | TEntity[], addOptions?: EntityAddOptions): TEntity[] {
42
        const addEntities = coerceArray(entity);
43
        if (addEntities.length === 0) {
×
44
            return this.entities;
×
45
        }
×
46
        if (addOptions && (addOptions.afterId || addOptions.prepend)) {
×
47
            if (addOptions.afterId) {
×
48
                const entities = [...this.entities];
×
49
                const index =
50
                    this.entities.findIndex(item => {
51
                        return item[this.idKey] === addOptions.afterId;
×
52
                    }) + 1;
53
                entities.splice(index, 0, ...addEntities);
54
                this.entities = [...entities];
×
55
            } else if (addOptions.prepend) {
×
56
                this.entities = [...addEntities, ...this.entities];
×
57
            }
58
        } else {
59
            this.entities = [...this.entities, ...addEntities];
60
        }
×
61
        return this.entities;
×
62
    }
×
63

64
    /**
65
     *
×
66
     * Update an entity or entities.
67
     *
68
     * @example
69
     * produce([users]).update(3, {
70
     *   name: 'New Name'
71
     * });
72
     *
73
     * produce([users]).update(3, entity => {
74
     *    return {
75
     *      ...entity,
76
     *      name: 'New Name'
×
77
     *    }
×
78
     *  });
×
79
     *
×
80
     * produce([users]).update([1,2,3], {
×
81
     *   name: 'New Name'
82
     * });
×
83
     */
×
84
    update(id: Id | Id[] | null, newStateFn: (entity: Readonly<TEntity>) => Partial<TEntity>): TEntity[];
×
85
    update(id: Id | Id[] | null, newState?: Partial<TEntity>): TEntity[];
86
    update(
×
87
        idsOrFn: Id | Id[] | null | Partial<TEntity> | ((entity: Readonly<TEntity>) => boolean),
×
88
        newStateOrFn?: ((entity: Readonly<TEntity>) => Partial<TEntity>) | Partial<TEntity>
89
    ): TEntity[] {
90
        const ids = coerceArray(idsOrFn);
×
91

×
92
        for (let i = 0; i < this.entities.length; i++) {
×
93
            const oldEntity = this.entities[i];
94
            if (ids.indexOf(oldEntity[this.idKey]) >= 0) {
95
                const newState = isFunction(newStateOrFn) ? (newStateOrFn as any)(oldEntity) : newStateOrFn;
×
96
                this.entities[i] = { ...(oldEntity as any), ...newState };
×
97
            }
×
98
        }
×
99
        return [...this.entities];
100
    }
×
101

×
102
    /**
103
     *
104
     * Remove one or more entities:
105
     *
106
     * @example
×
107
     * produce([users]).remove(5);
108
     * produce([users]).remove([1,2,3]);
109
     * produce([users]).remove(entity => entity.id === 1);
110
     */
111
    remove(id: Id | Id[]): TEntity[];
112
    remove(predicate: (entity: Readonly<TEntity>) => boolean): TEntity[];
113
    remove(idsOrFn?: Id | Id[] | ((entity: Readonly<TEntity>) => boolean)): TEntity[] {
114
        if (isFunction(idsOrFn)) {
115
            this.entities = this.entities.filter(entity => {
116
                return !(idsOrFn as any)(entity);
117
            });
118
        } else {
119
            const ids = coerceArray(idsOrFn);
120
            this.entities = this.entities.filter(entity => {
121
                return ids.indexOf(entity[this.idKey]) === -1;
122
            });
123
        }
124
        return this.entities;
125
    }
126

127
    /**
128
     *
129
     * Move one or more entities:
130
     *
131
     * @example
132
     * produce([users]).move(5, {afterId: 2});
133
     * produce([users]).move(5, {toIndex: 0});
134
     */
135
    move(id: Id, moveOptions?: EntityMoveOptions): TEntity[] {
136
        const fromIndex = this.entities.findIndex(item => item[this.idKey] === id);
137
        let toIndex = 0;
138
        const newEntities = [...this.entities];
139

140
        if (!id || fromIndex < 0) {
141
            return [...this.entities];
142
        }
143

144
        if (moveOptions) {
145
            if (!isUndefinedOrNull(moveOptions.afterId)) {
146
                toIndex = this.entities.findIndex(item => item[this.idKey] === moveOptions.afterId);
147
            } else if (moveOptions.toIndex) {
148
                toIndex = moveOptions.toIndex;
149
            }
150
        }
151
        toIndex = Math.max(0, Math.min(this.entities.length - 1, toIndex));
152
        if (toIndex === fromIndex) {
153
            return [...this.entities];
154
        } else {
155
            const target = this.entities[fromIndex];
156
            const delta = toIndex < fromIndex ? -1 : 1;
157

158
            for (let i = fromIndex; i !== toIndex; i += delta) {
159
                newEntities[i] = newEntities[i + delta];
160
            }
161
            newEntities[toIndex] = target;
162
            return [...newEntities];
163
        }
164
    }
165
}
166

167
export function produce<TEntity>(entities: TEntity[], options?: ProducerOptions) {
168
    return new Producer(entities, options);
169
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc