@etsoo/appscript 1.4.62 → 1.4.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,6 @@ import { Currency } from './Currency';
5
5
  * 购物篮所有人
6
6
  */
7
7
  export type ShoppingCartOwner = DataTypes.IdNameItem & {
8
- isSupplier?: boolean;
9
8
  culture?: string;
10
9
  currency?: Currency;
11
10
  };
@@ -20,6 +19,7 @@ export type ShoppingCartData<T extends ShoppingCartItem> = {
20
19
  items: T[];
21
20
  promotions: ShoppingPromotion[];
22
21
  formData?: any;
22
+ cache?: Record<string, unknown>;
23
23
  };
24
24
  /**
25
25
  * Shopping promotion
@@ -122,7 +122,7 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
122
122
  * @param key Additional key
123
123
  * @returns Result
124
124
  */
125
- static createKey(currency: Currency, culture: string, key?: string): string;
125
+ static createKey(currency: Currency, culture: string, key: string): string;
126
126
  /**
127
127
  * Clear shopping cart
128
128
  * 清除购物篮
@@ -130,14 +130,6 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
130
130
  * @param storage Storage
131
131
  */
132
132
  static clear(identifier: string, storage: IStorage): void;
133
- /**
134
- * Clear shopping cart
135
- * 清除购物篮
136
- * @param currency Currency
137
- * @param culture Culture
138
- * @param storage Storage
139
- */
140
- static clearWith(currency: Currency, culture: string, storage?: IStorage): void;
141
133
  /**
142
134
  * Get cart data
143
135
  * 获取购物篮数据
@@ -186,6 +178,11 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
186
178
  * 关联的表单数据
187
179
  */
188
180
  formData: any;
181
+ /**
182
+ * Cache
183
+ * 缓存对象
184
+ */
185
+ cache?: Record<string, unknown>;
189
186
  _symbol: string | undefined;
190
187
  /**
191
188
  * Currency symbol
@@ -193,6 +190,10 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
193
190
  */
194
191
  get symbol(): string | undefined;
195
192
  private set symbol(value);
193
+ /**
194
+ * Key for identifier
195
+ */
196
+ readonly key: string;
196
197
  /**
197
198
  * Cart identifier
198
199
  * 购物篮标识
@@ -237,17 +238,19 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
237
238
  /**
238
239
  * Constructor
239
240
  * 构造函数
241
+ * @param key Key for identifier
240
242
  * @param init Currency & culture ISO code array
241
243
  * @param storage Data storage
242
244
  */
243
- constructor(init: [Currency, string], storage?: IStorage);
245
+ constructor(key: string, init: [Currency, string], storage?: IStorage);
244
246
  /**
245
247
  * Constructor
246
248
  * 构造函数
249
+ * @param key Key for identifier
247
250
  * @param state Initialization state
248
251
  * @param storage Data storage
249
252
  */
250
- constructor(state: ShoppingCartData<T>, storage?: IStorage);
253
+ constructor(key: string, state: ShoppingCartData<T>, storage?: IStorage);
251
254
  private setCartData;
252
255
  private doChange;
253
256
  /**
@@ -295,12 +298,24 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
295
298
  * @returns Result
296
299
  */
297
300
  getItem(id: T['id']): T | undefined;
301
+ /**
302
+ * Push item
303
+ * 推送项目
304
+ * @param data Item data
305
+ * @returns Added or not
306
+ */
307
+ pushItem(data: T): boolean;
298
308
  /**
299
309
  * Reset currency and culture
300
310
  * @param currency New currency
301
311
  * @param culture New culture
302
312
  */
303
313
  reset(currency: Currency, culture: string): void;
314
+ /**
315
+ * Remove item from the index
316
+ * @param index Item index
317
+ */
318
+ removeItem(index: number): void;
304
319
  /**
305
320
  * Reset item
306
321
  * @param item Shopping cart item
@@ -16,7 +16,7 @@ class ShoppingCart {
16
16
  * @param key Additional key
17
17
  * @returns Result
18
18
  */
19
- static createKey(currency, culture, key = 'KEY') {
19
+ static createKey(currency, culture, key) {
20
20
  return `ETSOO-CART-${culture}-${key}-${currency}`;
21
21
  }
22
22
  /**
@@ -34,17 +34,6 @@ class ShoppingCart {
34
34
  console.log('ShoppingCart clear', error);
35
35
  }
36
36
  }
37
- /**
38
- * Clear shopping cart
39
- * 清除购物篮
40
- * @param currency Currency
41
- * @param culture Culture
42
- * @param storage Storage
43
- */
44
- static clearWith(currency, culture, storage = new shared_1.WindowStorage()) {
45
- const identifier = this.createKey(currency, culture);
46
- this.clear(identifier, storage);
47
- }
48
37
  /**
49
38
  * Get cart data
50
39
  * 获取购物篮数据
@@ -135,7 +124,7 @@ class ShoppingCart {
135
124
  */
136
125
  get identifier() {
137
126
  const o = this.owner;
138
- return ShoppingCart.createKey(this.currency, this.culture, o ? `${o.isSupplier ? 'S' : 'C'}${o.id}` : undefined);
127
+ return ShoppingCart.createKey(this.currency, this.culture, this.key);
139
128
  }
140
129
  /**
141
130
  * All data keys
@@ -182,10 +171,12 @@ class ShoppingCart {
182
171
  /**
183
172
  * Constructor
184
173
  * 构造函数
174
+ * @param key Key for identifier
185
175
  * @param currency Currency ISO code
186
176
  * @param storage Data storage
187
177
  */
188
- constructor(currencyOrState, storage = new shared_1.WindowStorage()) {
178
+ constructor(key, currencyOrState, storage = new shared_1.WindowStorage()) {
179
+ var _a;
189
180
  this.storage = storage;
190
181
  this._items = [];
191
182
  this._promotions = [];
@@ -194,9 +185,11 @@ class ShoppingCart {
194
185
  * 缓存的价格
195
186
  */
196
187
  this.prices = {};
188
+ this.key = key;
197
189
  if (Array.isArray(currencyOrState)) {
198
190
  this.changeCurrency(currencyOrState[0]);
199
191
  this.changeCulture(currencyOrState[1]);
192
+ this.setCartData((_a = this.storage.getPersistedObject(this.identifier)) !== null && _a !== void 0 ? _a : this.storage.getObject(this.identifier));
200
193
  }
201
194
  else {
202
195
  this.setCartData(currencyOrState);
@@ -205,11 +198,12 @@ class ShoppingCart {
205
198
  }
206
199
  }
207
200
  setCartData(state) {
208
- const { owner, items = [], promotions = [], formData } = state !== null && state !== void 0 ? state : {};
201
+ const { owner, items = [], promotions = [], formData, cache } = state !== null && state !== void 0 ? state : {};
209
202
  this.owner = owner;
210
203
  this.items = items;
211
204
  this.promotions = promotions;
212
205
  this.formData = formData;
206
+ this.cache = cache;
213
207
  }
214
208
  doChange(reason, changedItems) {
215
209
  if (this.onChange)
@@ -294,6 +288,21 @@ class ShoppingCart {
294
288
  getItem(id) {
295
289
  return this.items.find((item) => item.id === id);
296
290
  }
291
+ /**
292
+ * Push item
293
+ * 推送项目
294
+ * @param data Item data
295
+ * @returns Added or not
296
+ */
297
+ pushItem(data) {
298
+ if (this.items.some((item) => item.id === data.id)) {
299
+ return false;
300
+ }
301
+ else {
302
+ this.addItem(data);
303
+ return true;
304
+ }
305
+ }
297
306
  /**
298
307
  * Reset currency and culture
299
308
  * @param currency New currency
@@ -304,6 +313,14 @@ class ShoppingCart {
304
313
  this.changeCurrency(currency);
305
314
  this.changeCulture(culture);
306
315
  }
316
+ /**
317
+ * Remove item from the index
318
+ * @param index Item index
319
+ */
320
+ removeItem(index) {
321
+ const removedItems = this.items.splice(index, 1);
322
+ this.doChange('remove', removedItems);
323
+ }
307
324
  /**
308
325
  * Reset item
309
326
  * @param item Shopping cart item
@@ -320,14 +337,15 @@ class ShoppingCart {
320
337
  save(persisted = true) {
321
338
  if (this.owner == null)
322
339
  return;
323
- const { currency, culture, owner, items, promotions, formData } = this;
340
+ const { currency, culture, owner, items, promotions, formData, cache } = this;
324
341
  const data = {
325
342
  currency,
326
343
  culture,
327
344
  owner,
328
345
  items,
329
346
  promotions,
330
- formData
347
+ formData,
348
+ cache
331
349
  };
332
350
  try {
333
351
  if (persisted) {
@@ -425,8 +443,7 @@ class ShoppingCart {
425
443
  if (qty == null) {
426
444
  // Remove the item
427
445
  if (index !== -1) {
428
- const removedItems = this.items.splice(index, 1);
429
- this.doChange('remove', removedItems);
446
+ this.removeItem(index);
430
447
  }
431
448
  }
432
449
  else if (index === -1) {
@@ -5,7 +5,6 @@ import { Currency } from './Currency';
5
5
  * 购物篮所有人
6
6
  */
7
7
  export type ShoppingCartOwner = DataTypes.IdNameItem & {
8
- isSupplier?: boolean;
9
8
  culture?: string;
10
9
  currency?: Currency;
11
10
  };
@@ -20,6 +19,7 @@ export type ShoppingCartData<T extends ShoppingCartItem> = {
20
19
  items: T[];
21
20
  promotions: ShoppingPromotion[];
22
21
  formData?: any;
22
+ cache?: Record<string, unknown>;
23
23
  };
24
24
  /**
25
25
  * Shopping promotion
@@ -122,7 +122,7 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
122
122
  * @param key Additional key
123
123
  * @returns Result
124
124
  */
125
- static createKey(currency: Currency, culture: string, key?: string): string;
125
+ static createKey(currency: Currency, culture: string, key: string): string;
126
126
  /**
127
127
  * Clear shopping cart
128
128
  * 清除购物篮
@@ -130,14 +130,6 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
130
130
  * @param storage Storage
131
131
  */
132
132
  static clear(identifier: string, storage: IStorage): void;
133
- /**
134
- * Clear shopping cart
135
- * 清除购物篮
136
- * @param currency Currency
137
- * @param culture Culture
138
- * @param storage Storage
139
- */
140
- static clearWith(currency: Currency, culture: string, storage?: IStorage): void;
141
133
  /**
142
134
  * Get cart data
143
135
  * 获取购物篮数据
@@ -186,6 +178,11 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
186
178
  * 关联的表单数据
187
179
  */
188
180
  formData: any;
181
+ /**
182
+ * Cache
183
+ * 缓存对象
184
+ */
185
+ cache?: Record<string, unknown>;
189
186
  _symbol: string | undefined;
190
187
  /**
191
188
  * Currency symbol
@@ -193,6 +190,10 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
193
190
  */
194
191
  get symbol(): string | undefined;
195
192
  private set symbol(value);
193
+ /**
194
+ * Key for identifier
195
+ */
196
+ readonly key: string;
196
197
  /**
197
198
  * Cart identifier
198
199
  * 购物篮标识
@@ -237,17 +238,19 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
237
238
  /**
238
239
  * Constructor
239
240
  * 构造函数
241
+ * @param key Key for identifier
240
242
  * @param init Currency & culture ISO code array
241
243
  * @param storage Data storage
242
244
  */
243
- constructor(init: [Currency, string], storage?: IStorage);
245
+ constructor(key: string, init: [Currency, string], storage?: IStorage);
244
246
  /**
245
247
  * Constructor
246
248
  * 构造函数
249
+ * @param key Key for identifier
247
250
  * @param state Initialization state
248
251
  * @param storage Data storage
249
252
  */
250
- constructor(state: ShoppingCartData<T>, storage?: IStorage);
253
+ constructor(key: string, state: ShoppingCartData<T>, storage?: IStorage);
251
254
  private setCartData;
252
255
  private doChange;
253
256
  /**
@@ -295,12 +298,24 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
295
298
  * @returns Result
296
299
  */
297
300
  getItem(id: T['id']): T | undefined;
301
+ /**
302
+ * Push item
303
+ * 推送项目
304
+ * @param data Item data
305
+ * @returns Added or not
306
+ */
307
+ pushItem(data: T): boolean;
298
308
  /**
299
309
  * Reset currency and culture
300
310
  * @param currency New currency
301
311
  * @param culture New culture
302
312
  */
303
313
  reset(currency: Currency, culture: string): void;
314
+ /**
315
+ * Remove item from the index
316
+ * @param index Item index
317
+ */
318
+ removeItem(index: number): void;
304
319
  /**
305
320
  * Reset item
306
321
  * @param item Shopping cart item
@@ -13,7 +13,7 @@ export class ShoppingCart {
13
13
  * @param key Additional key
14
14
  * @returns Result
15
15
  */
16
- static createKey(currency, culture, key = 'KEY') {
16
+ static createKey(currency, culture, key) {
17
17
  return `ETSOO-CART-${culture}-${key}-${currency}`;
18
18
  }
19
19
  /**
@@ -31,17 +31,6 @@ export class ShoppingCart {
31
31
  console.log('ShoppingCart clear', error);
32
32
  }
33
33
  }
34
- /**
35
- * Clear shopping cart
36
- * 清除购物篮
37
- * @param currency Currency
38
- * @param culture Culture
39
- * @param storage Storage
40
- */
41
- static clearWith(currency, culture, storage = new WindowStorage()) {
42
- const identifier = this.createKey(currency, culture);
43
- this.clear(identifier, storage);
44
- }
45
34
  /**
46
35
  * Get cart data
47
36
  * 获取购物篮数据
@@ -132,7 +121,7 @@ export class ShoppingCart {
132
121
  */
133
122
  get identifier() {
134
123
  const o = this.owner;
135
- return ShoppingCart.createKey(this.currency, this.culture, o ? `${o.isSupplier ? 'S' : 'C'}${o.id}` : undefined);
124
+ return ShoppingCart.createKey(this.currency, this.culture, this.key);
136
125
  }
137
126
  /**
138
127
  * All data keys
@@ -179,10 +168,12 @@ export class ShoppingCart {
179
168
  /**
180
169
  * Constructor
181
170
  * 构造函数
171
+ * @param key Key for identifier
182
172
  * @param currency Currency ISO code
183
173
  * @param storage Data storage
184
174
  */
185
- constructor(currencyOrState, storage = new WindowStorage()) {
175
+ constructor(key, currencyOrState, storage = new WindowStorage()) {
176
+ var _a;
186
177
  this.storage = storage;
187
178
  this._items = [];
188
179
  this._promotions = [];
@@ -191,9 +182,11 @@ export class ShoppingCart {
191
182
  * 缓存的价格
192
183
  */
193
184
  this.prices = {};
185
+ this.key = key;
194
186
  if (Array.isArray(currencyOrState)) {
195
187
  this.changeCurrency(currencyOrState[0]);
196
188
  this.changeCulture(currencyOrState[1]);
189
+ this.setCartData((_a = this.storage.getPersistedObject(this.identifier)) !== null && _a !== void 0 ? _a : this.storage.getObject(this.identifier));
197
190
  }
198
191
  else {
199
192
  this.setCartData(currencyOrState);
@@ -202,11 +195,12 @@ export class ShoppingCart {
202
195
  }
203
196
  }
204
197
  setCartData(state) {
205
- const { owner, items = [], promotions = [], formData } = state !== null && state !== void 0 ? state : {};
198
+ const { owner, items = [], promotions = [], formData, cache } = state !== null && state !== void 0 ? state : {};
206
199
  this.owner = owner;
207
200
  this.items = items;
208
201
  this.promotions = promotions;
209
202
  this.formData = formData;
203
+ this.cache = cache;
210
204
  }
211
205
  doChange(reason, changedItems) {
212
206
  if (this.onChange)
@@ -291,6 +285,21 @@ export class ShoppingCart {
291
285
  getItem(id) {
292
286
  return this.items.find((item) => item.id === id);
293
287
  }
288
+ /**
289
+ * Push item
290
+ * 推送项目
291
+ * @param data Item data
292
+ * @returns Added or not
293
+ */
294
+ pushItem(data) {
295
+ if (this.items.some((item) => item.id === data.id)) {
296
+ return false;
297
+ }
298
+ else {
299
+ this.addItem(data);
300
+ return true;
301
+ }
302
+ }
294
303
  /**
295
304
  * Reset currency and culture
296
305
  * @param currency New currency
@@ -301,6 +310,14 @@ export class ShoppingCart {
301
310
  this.changeCurrency(currency);
302
311
  this.changeCulture(culture);
303
312
  }
313
+ /**
314
+ * Remove item from the index
315
+ * @param index Item index
316
+ */
317
+ removeItem(index) {
318
+ const removedItems = this.items.splice(index, 1);
319
+ this.doChange('remove', removedItems);
320
+ }
304
321
  /**
305
322
  * Reset item
306
323
  * @param item Shopping cart item
@@ -317,14 +334,15 @@ export class ShoppingCart {
317
334
  save(persisted = true) {
318
335
  if (this.owner == null)
319
336
  return;
320
- const { currency, culture, owner, items, promotions, formData } = this;
337
+ const { currency, culture, owner, items, promotions, formData, cache } = this;
321
338
  const data = {
322
339
  currency,
323
340
  culture,
324
341
  owner,
325
342
  items,
326
343
  promotions,
327
- formData
344
+ formData,
345
+ cache
328
346
  };
329
347
  try {
330
348
  if (persisted) {
@@ -422,8 +440,7 @@ export class ShoppingCart {
422
440
  if (qty == null) {
423
441
  // Remove the item
424
442
  if (index !== -1) {
425
- const removedItems = this.items.splice(index, 1);
426
- this.doChange('remove', removedItems);
443
+ this.removeItem(index);
427
444
  }
428
445
  }
429
446
  else if (index === -1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.4.62",
3
+ "version": "1.4.64",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -6,7 +6,6 @@ import { Currency } from './Currency';
6
6
  * 购物篮所有人
7
7
  */
8
8
  export type ShoppingCartOwner = DataTypes.IdNameItem & {
9
- isSupplier?: boolean;
10
9
  culture?: string;
11
10
  currency?: Currency;
12
11
  };
@@ -22,6 +21,7 @@ export type ShoppingCartData<T extends ShoppingCartItem> = {
22
21
  items: T[];
23
22
  promotions: ShoppingPromotion[];
24
23
  formData?: any;
24
+ cache?: Record<string, unknown>;
25
25
  };
26
26
 
27
27
  /**
@@ -145,7 +145,7 @@ export class ShoppingCart<T extends ShoppingCartItem> {
145
145
  * @param key Additional key
146
146
  * @returns Result
147
147
  */
148
- static createKey(currency: Currency, culture: string, key: string = 'KEY') {
148
+ static createKey(currency: Currency, culture: string, key: string) {
149
149
  return `ETSOO-CART-${culture}-${key}-${currency}`;
150
150
  }
151
151
 
@@ -164,22 +164,6 @@ export class ShoppingCart<T extends ShoppingCartItem> {
164
164
  }
165
165
  }
166
166
 
167
- /**
168
- * Clear shopping cart
169
- * 清除购物篮
170
- * @param currency Currency
171
- * @param culture Culture
172
- * @param storage Storage
173
- */
174
- static clearWith(
175
- currency: Currency,
176
- culture: string,
177
- storage: IStorage = new WindowStorage()
178
- ) {
179
- const identifier = this.createKey(currency, culture);
180
- this.clear(identifier, storage);
181
- }
182
-
183
167
  /**
184
168
  * Get cart data
185
169
  * 获取购物篮数据
@@ -277,6 +261,12 @@ export class ShoppingCart<T extends ShoppingCartItem> {
277
261
  */
278
262
  formData: any;
279
263
 
264
+ /**
265
+ * Cache
266
+ * 缓存对象
267
+ */
268
+ cache?: Record<string, unknown>;
269
+
280
270
  _symbol: string | undefined;
281
271
  /**
282
272
  * Currency symbol
@@ -289,17 +279,18 @@ export class ShoppingCart<T extends ShoppingCartItem> {
289
279
  this._symbol = value;
290
280
  }
291
281
 
282
+ /**
283
+ * Key for identifier
284
+ */
285
+ readonly key: string;
286
+
292
287
  /**
293
288
  * Cart identifier
294
289
  * 购物篮标识
295
290
  */
296
291
  get identifier() {
297
292
  const o = this.owner;
298
- return ShoppingCart.createKey(
299
- this.currency,
300
- this.culture,
301
- o ? `${o.isSupplier ? 'S' : 'C'}${o.id}` : undefined
302
- );
293
+ return ShoppingCart.createKey(this.currency, this.culture, this.key);
303
294
  }
304
295
 
305
296
  /**
@@ -369,32 +360,43 @@ export class ShoppingCart<T extends ShoppingCartItem> {
369
360
  /**
370
361
  * Constructor
371
362
  * 构造函数
363
+ * @param key Key for identifier
372
364
  * @param init Currency & culture ISO code array
373
365
  * @param storage Data storage
374
366
  */
375
- constructor(init: [Currency, string], storage?: IStorage);
367
+ constructor(key: string, init: [Currency, string], storage?: IStorage);
376
368
 
377
369
  /**
378
370
  * Constructor
379
371
  * 构造函数
372
+ * @param key Key for identifier
380
373
  * @param state Initialization state
381
374
  * @param storage Data storage
382
375
  */
383
- constructor(state: ShoppingCartData<T>, storage?: IStorage);
376
+ constructor(key: string, state: ShoppingCartData<T>, storage?: IStorage);
384
377
 
385
378
  /**
386
379
  * Constructor
387
380
  * 构造函数
381
+ * @param key Key for identifier
388
382
  * @param currency Currency ISO code
389
383
  * @param storage Data storage
390
384
  */
391
385
  constructor(
386
+ key: string,
392
387
  currencyOrState: [Currency, string] | ShoppingCartData<T>,
393
388
  private readonly storage: IStorage = new WindowStorage()
394
389
  ) {
390
+ this.key = key;
391
+
395
392
  if (Array.isArray(currencyOrState)) {
396
393
  this.changeCurrency(currencyOrState[0]);
397
394
  this.changeCulture(currencyOrState[1]);
395
+
396
+ this.setCartData(
397
+ this.storage.getPersistedObject(this.identifier) ??
398
+ this.storage.getObject(this.identifier)
399
+ );
398
400
  } else {
399
401
  this.setCartData(currencyOrState);
400
402
  this.changeCurrency(currencyOrState.currency);
@@ -403,11 +405,18 @@ export class ShoppingCart<T extends ShoppingCartItem> {
403
405
  }
404
406
 
405
407
  private setCartData(state: ShoppingCartData<T> | undefined) {
406
- const { owner, items = [], promotions = [], formData } = state ?? {};
408
+ const {
409
+ owner,
410
+ items = [],
411
+ promotions = [],
412
+ formData,
413
+ cache
414
+ } = state ?? {};
407
415
  this.owner = owner;
408
416
  this.items = items;
409
417
  this.promotions = promotions;
410
418
  this.formData = formData;
419
+ this.cache = cache;
411
420
  }
412
421
 
413
422
  private doChange(reason: ShoppingCartChangeReason, changedItems: T[]) {
@@ -503,6 +512,21 @@ export class ShoppingCart<T extends ShoppingCartItem> {
503
512
  return this.items.find((item) => item.id === id);
504
513
  }
505
514
 
515
+ /**
516
+ * Push item
517
+ * 推送项目
518
+ * @param data Item data
519
+ * @returns Added or not
520
+ */
521
+ pushItem(data: T) {
522
+ if (this.items.some((item) => item.id === data.id)) {
523
+ return false;
524
+ } else {
525
+ this.addItem(data);
526
+ return true;
527
+ }
528
+ }
529
+
506
530
  /**
507
531
  * Reset currency and culture
508
532
  * @param currency New currency
@@ -514,6 +538,15 @@ export class ShoppingCart<T extends ShoppingCartItem> {
514
538
  this.changeCulture(culture);
515
539
  }
516
540
 
541
+ /**
542
+ * Remove item from the index
543
+ * @param index Item index
544
+ */
545
+ removeItem(index: number) {
546
+ const removedItems = this.items.splice(index, 1);
547
+ this.doChange('remove', removedItems);
548
+ }
549
+
517
550
  /**
518
551
  * Reset item
519
552
  * @param item Shopping cart item
@@ -531,14 +564,16 @@ export class ShoppingCart<T extends ShoppingCartItem> {
531
564
  save(persisted: boolean = true) {
532
565
  if (this.owner == null) return;
533
566
 
534
- const { currency, culture, owner, items, promotions, formData } = this;
567
+ const { currency, culture, owner, items, promotions, formData, cache } =
568
+ this;
535
569
  const data: ShoppingCartData<T> = {
536
570
  currency,
537
571
  culture,
538
572
  owner,
539
573
  items,
540
574
  promotions,
541
- formData
575
+ formData,
576
+ cache
542
577
  };
543
578
 
544
579
  try {
@@ -655,8 +690,7 @@ export class ShoppingCart<T extends ShoppingCartItem> {
655
690
  if (qty == null) {
656
691
  // Remove the item
657
692
  if (index !== -1) {
658
- const removedItems = this.items.splice(index, 1);
659
- this.doChange('remove', removedItems);
693
+ this.removeItem(index);
660
694
  }
661
695
  } else if (index === -1) {
662
696
  // New