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

jclo / spine / 10813910100

11 Sep 2024 02:36PM UTC coverage: 89.334% (-0.1%) from 89.471%
10813910100

push

github

jclo
Fixed regex on $initialize, $listen, etc. that failed on minified library.

447 of 522 branches covered (85.63%)

Branch coverage included in aggregate %.

8 of 9 new or added lines in 4 files covered. (88.89%)

10 existing lines in 3 files now uncovered.

4101 of 4569 relevant lines covered (89.76%)

4.56 hits per line

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

88.85
/src/components/model/main.js
1
/** ************************************************************************
1✔
2
 *
1✔
3
 * Defines Spine.Model.
1✔
4
 *
1✔
5
 * pseudoclassical-auto.js is built upon a variation of the Pseudoclassical
1✔
6
 * Instantiation pattern. The object is instantiated by the new keyword
1✔
7
 * included in the constructor. The caller just needs to call the
1✔
8
 * constructor without the new keyword to get in return the object.
1✔
9
 *
3✔
10
 * Private Functions:
1✔
11
 *  . none,
1✔
12
 *
1✔
13
 *
1✔
14
 * Constructor:
1✔
15
 *  . Model                       returns the extended Spine.Model object,
1✔
16
 *
1✔
17
 *
1✔
18
 * Private Methods:
1✔
19
 *  . _intInitialize              checks if it needs to use $initialize or initialize,
1✔
20
 *  . _intListen                  checks if it needs to use $listen or listen,
1✔
21
 *  . _intParse                   checks if it needs to use $parse or parse,
1✔
22
 *  . _init                       makes private init. after creation,
1✔
23
 *  . _parse                      parses the received object.
1✔
24
 *
1✔
25
 *
1✔
26
 * Empty Public Methods:
1✔
27
 *  . $initialize                 makes extra initializations,
1✔
28
 *  . $listen                     listens for events,
1✔
29
 *  . $parse                      parses the downloaded object or bypass,
1✔
30
 *
1✔
31
 *
1✔
32
 * Public Methods:
1✔
33
 *  . $get                        returns the value of the req. model property,
1✔
34
 *  . $getAll                     returns an object with all the key/values,
1✔
35
 *  . $set                        sets or updates model properties,
1✔
36
 *  . $remove                     removes model property(ies),
1✔
37
 *  . $has                        checks if the model has the property,
1✔
38
 *  . $fetch                      retrieves a model from the server,
1✔
39
 *  . $save                       sends a model to the server,
1✔
40
 *  . $delete                     deletes a model from the server,
1✔
41
 *  . $urify                      extends url with query parameters,
1✔
42
 *
1✔
43
 *
1✔
44
 *
1✔
45
 * @namespace    Spine
1✔
46
 * @dependencies none
1✔
47
 * @exports      -
1✔
48
 * @author       -
1✔
49
 * @since        0.0.0
1✔
50
 * @version      -
1✔
51
 * ********************************************************************** */
1✔
52
/* global */
1✔
53
/* eslint-disable one-var, semi-style, no-underscore-dangle */
1✔
54

1✔
55

1✔
56
// -- Vendor Modules
1✔
57
import KZlog from '@mobilabs/kzlog';
1✔
58

1✔
59

1✔
60
// -- Local Modules
1✔
61
import config from '../../config';
1✔
62
import _ from '../../libs/_';
1✔
63
import Generic from '../generic/main';
1✔
64
import U from '../../utils/util1';
1✔
65
import F from './private/fetch';
1✔
66
import U1 from './private/util';
1✔
67

1✔
68

1✔
69
// -- Local Constants
1✔
70
const { level } = config.logger
1✔
71
    , log       = KZlog('Spine', level, false)
1✔
72
    ;
1✔
73

1✔
74

1✔
75
// -- Local Variables
1✔
76
let mmethods;
1✔
77

1✔
78

1✔
79
// -- Public ---------------------------------------------------------------
1✔
80

1✔
81
/**
1✔
82
 * Returns the Spine.Model object.
1✔
83
 * (Pseudoclassical Instantation Pattern with auto instantatiation
1✔
84
 * - no need for new)
1✔
85
 *
1✔
86
 * @constructor (arg1)
1✔
87
 * @public
1✔
88
 * @param {String}          the argument to be saved as an object variable,
1✔
89
 * @returns {Object}        returns the Spine object,
1✔
90
 * @since 0.0.0
1✔
91
 */
1✔
92
/* eslint-disable prefer-spread, prefer-rest-params */
1✔
93
const Model = function(methods) {
1✔
94
  let args;
1✔
95
  const Child = function() {
1✔
96
    if (this instanceof Child) {
1✔
97
      Generic.Construct.apply(this, args);
1✔
98
      this._intInitialize.apply(this, args);
1✔
99
      this._intListen.apply(this);
1✔
100
      return this;
1✔
101
    }
1✔
102
    args = arguments;
1✔
103
    return new Child();
1✔
104
  };
1✔
105

1✔
106
  // We created our own assign method as Object.assign does not preserve
1✔
107
  // the getters and setters. So, do not use Object.assign here! And, do not
1✔
108
  // do this 'Child.prototype = _.assign(Generic.methods, methods)'! You will
1✔
109
  // copy the references instead of cloning the methods. And all the childs
276✔
110
  // will get the methods of the last created child.
138✔
111
  const p1 = _.assign({}, Generic.methods);
138✔
112
  const p2 = _.assign(p1, mmethods);
138✔
113
  Child.prototype = _.assign(p2, methods || {});
62✔
114
  Child.prototype.constructor = Child;
62✔
115
  return Child;
62✔
116
};
62✔
117
/* eslint-enable prefer-spread, prefer-rest-params */
62✔
118

62✔
119

62✔
120
// -- Public Methods -------------------------------------------------------
62✔
121

62✔
122
mmethods = {
62✔
123

62✔
124
  // -- Private Methods ----------------------------------------------------
62✔
125

62✔
126
  /**
62✔
127
   * Checks if it needs to use $initialize or the deprecated initialize method.
62✔
128
   *
62✔
129
   * @method ()
62✔
130
   * @private
62✔
131
   * @param {}              -,
62✔
132
   * @returns {}            -,
62✔
133
   * @since 0.0.0
62✔
134
   */
62✔
135
  _intInitialize(...args) {
1✔
136
    if (!/^initialize\(\)\s*\{\}/.test(this.initialize.toString())) {
1✔
137
      log.warn('initialize method is deprecated, use $initialize instead!');
1✔
138
      this.initialize(...args);
1✔
139
      return;
1✔
140
    }
1✔
141
    this.$initialize(...args);
1✔
142
  },
1✔
143

1✔
144
  /**
1✔
145
   * Checks if it needs to use $listen or the deprecated listen method.
1✔
146
   *
1✔
147
   * @method ()
1✔
148
   * @private
1✔
149
   * @param {}              -,
1✔
150
   * @returns {}            -,
1✔
151
   * @since 0.0.0
1✔
152
   */
1✔
153
  _intListen() {
138✔
154
    if (!/^listen\(\)\s*\{\}/.test(this.listen.toString())) {
138✔
155
      log.warn('listen method is deprecated, use $listen instead!');
138!
156
      this.listen();
×
157
      return;
×
158
    }
×
UNCOV
159
    this.$listen();
×
UNCOV
160
  },
×
UNCOV
161

×
UNCOV
162
  /**
×
163
   * Checks if it needs to use $parse or the deprecated parse method.
1✔
164
   *
1✔
165
   * @method ()
1✔
166
   * @private
1✔
167
   * @param {}              -,
1✔
168
   * @returns {}            -,
1✔
169
   * @since 0.0.0
1✔
170
   */
1✔
171
  _intParse(...args) {
1✔
172
    if (!/^parse\(\)\s*\{\}/.test(this.parse.toString())) {
1✔
173
      log.warn('parse method is deprecated, use $parse instead!');
138!
174
      return this.parse(...args);
×
175
    }
×
UNCOV
176
    return this.$parse(...args);
×
UNCOV
177
  },
×
UNCOV
178

×
179
  /**
138✔
180
   * Makes initializations when the object is constructed.
1✔
181
   *
1✔
182
   * @method (...args)
1✔
183
   * @private
1✔
184
   * @param {}              -,
1✔
185
   * @returns {Object}      return this,
1✔
186
   * @since 0.0.0
1✔
187
   */
1✔
188
  _init(...args) {
1✔
189
    this.url = this.url || null;
1✔
190
    this.defaults = this.defaults || {};
3✔
191
    const [obj, options] = args;
3!
192
    this._attributes = this._parse(obj, options);
×
193
    return this;
×
194
  },
×
195

×
196
  /**
×
197
   * Parses the received object.
3✔
198
   *
3✔
199
   * @method (arg1, arg2)
1✔
200
   * @private
1✔
201
   * @param {Object}        the received object,
1✔
202
   * @param {Object}        the options,
1✔
203
   * @returns {Object}      return the parsed object,
1✔
204
   * @since 0.0.0
1✔
205
   */
1✔
206
  /* eslint-disable no-restricted-syntax */
1✔
207
  _parse(data, options) {
138✔
208
    let obj = data || {};
138✔
209
    obj = options && options.parse ? this._intParse(obj) : obj;
138✔
210
    for (const item in this.defaults) {
138✔
211
      if (!obj[item]) {
1✔
212
        obj[item] = this.defaults[item];
1✔
213
      }
1✔
214
    }
1✔
215
    return obj;
1✔
216
  },
1✔
217
  /* eslint-enable no-restricted-syntax */
1✔
218

1✔
219

1✔
220
  // -- Empty Public Methods -----------------------------------------------
1✔
221

1✔
222
  /**
1✔
223
   * Makes the initializations.
1✔
224
   * (empty public method - could be overwritten)
1✔
225
   *
148✔
226
   * @method ()
148✔
227
   * @public
148✔
228
   * @param {}              -,
148✔
229
   * @returns {Object}      returns this,
148✔
230
   * @since 0.0.0
148✔
231
   */
148✔
232
  initialize() {},
148✔
233
  $initialize() {
2✔
234
    return this;
1✔
235
  },
1✔
236

1✔
237
  /**
1✔
238
   * Listens for events.
2✔
239
   * (empty public method - could be overwritten)
1✔
240
   *
1✔
241
   * @method ()
1✔
242
   * @public
1✔
243
   * @param {}              -,
1✔
244
   * @returns {Object}      returns this,
1✔
245
   * @since 0.0.0
1✔
246
   */
1✔
247
  listen() {},
1✔
248
  $listen() {
1✔
249
    return this;
1✔
250
  },
1✔
251

1✔
252
  /**
1✔
253
   * Parses the downloaded file.
1✔
254
   * (empty public method - could be overwritten)
1✔
255
   *
1✔
256
   * @method (arg1)
1✔
257
   * @public
1✔
258
   * @param {String}        the downloaded json file,
1✔
259
   * @returns {String}      returns the downloaded file,
1✔
260
   * @since 0.0.0
1✔
261
   */
1✔
262
  parse() {},
1✔
263
  $parse(attributes) {
1✔
264
    return attributes;
1✔
265
  },
1✔
266

1✔
267

1✔
268
  // -- Public Methods -----------------------------------------------------
1✔
269

1✔
270
  /**
×
271
   * Returns the value of the requested model property.
1✔
272
   * (public method - must not be overwritten)
1✔
273
   *
1✔
274
   * @method (arg1)
1✔
275
   * @public
1✔
276
   * @param {String}        the model property,
1✔
277
   * @returns {...}         returns the value of this property,
1✔
278
   * @since 0.0.0
1✔
279
   */
1✔
280
  $get(prop) {
1✔
281
    return typeof prop === 'string' ? this._attributes[prop] : null;
1✔
282
  },
2✔
283
  get(prop) {
1✔
284
    log.warn('get method is deprecated, use $get instead!');
1✔
285
    return this.$get(prop);
1✔
286
  },
1✔
287

1✔
288
  /**
1✔
289
   * Returns an object containing all the model properties.
1✔
290
   * (public method - must not be overwritten)
1✔
291
   *
1✔
292
   * @method ()
1✔
293
   * @public
1✔
294
   * @param {}              -,
1✔
295
   * @returns {Object}      returns the model properties,
1✔
296
   * @since 0.0.0
1✔
297
   */
1✔
298
  $getAll() {
1✔
299
    return { ...this._attributes };
108✔
300
  },
107✔
301
  getAll() {
107✔
302
    log.warn('getAll method is deprecated, use $getAll instead!');
1✔
303
    return this.$getAll();
×
304
  },
×
305

×
306
  /**
×
307
   * Sets or updates model properties.
1✔
308
   * (public method - must not be overwritten)
1✔
309
   *
1✔
310
   * Nota:
1✔
311
   * Fires a 'change' event if not silent ({ silent: true }).
1✔
312
   *
1✔
313
   * @method (arg1, arg2)
1✔
314
   * @public
1✔
315
   * @param {Object/String} the properties or one property,
1✔
316
   * @returns {Object/.}    the options or the property value,
1✔
317
   * @since 0.0.0
1✔
318
   */
×
319
  $set(...args) {
×
320
    return U1.set(this, ...args);
×
321
  },
×
322
  set(...args) {
×
323
    log.warn('set method is deprecated, use $set instead!');
1✔
324
    return this.$set(...args);
1✔
325
  },
1✔
326

1✔
327
  /**
1✔
328
   * Removes one or a set of model property(ies).
1✔
329
   * (public method - must not be overwritten)
1✔
330
   *
1✔
331
   * Nota:
1✔
332
   * It removes a property or a set of properties from a model but not from
1✔
333
   * the server. It fires the 'remove:prop' event for each property removed
1✔
334
   * and a 'remove' afterwards. Firing could be disabled with the option
1✔
335
   * { silent: true }.
×
336
   * if you want to update the model on the server too, you need to call
×
337
   * the 'save' method.
1✔
338
   *
1✔
339
   * @method (arg1, arg2)
1✔
340
   * @public
1✔
341
   * @param {Object/String} the properties or one property,
1✔
342
   * @returns {Object}      returns the removed properties and their values,
1✔
343
   * @since 0.0.0
1✔
344
   */
1✔
345
  $remove(...args) {
1✔
346
    return U1.remove(this, ...args);
1✔
347
  },
1✔
348
  remove(...args) {
1✔
349
    log.warn('remove method is deprecated, use $remove instead!');
1✔
350
    return this.$remove(...args);
1✔
351
  },
1✔
352

1✔
353
  /**
1✔
354
   * Checks if the model has the passed-in property.
1✔
355
   * (public method - must not be overwritten)
1✔
356
   *
1✔
357
   * @method (arg1)
1✔
358
   * @public
1✔
359
   * @param {String}        the property,
1✔
360
   * @returns {Boolean}     returns true if the property exist otherwise false,
1✔
361
   * @since 0.0.0
1✔
362
   */
×
363
  $has(prop) {
×
364
    if (typeof prop === 'string' && prop in this._attributes) {
1✔
365
      return true;
×
366
    }
×
367
    return false;
×
368
  },
×
369
  has(prop) {
×
370
    log.warn('has method is deprecated, use $has instead!');
1✔
371
    return this.$has(prop);
1✔
372
  },
1✔
373

1✔
374
  /**
1✔
375
   * Retrieves a model from the server.
1✔
376
   * (public method - must not be overwritten)
1✔
377
   *
1✔
378
   * Nota:
1✔
379
   * Fires a 'load' event if not silent ({ silent: true }).
1✔
380
   *
1✔
381
   * @method ([arg1], [arg2])
1✔
382
   * @public
1✔
383
   * @param {Object}        the options,
1✔
384
   * @param {Function}      the function to call at the completion,
2✔
385
   * @returns {Object}      returns this,
1✔
386
   * @since 0.0.0
×
387
   */
×
388
  $fetch(...args) {
×
389
    F.fetch(this, this.url, ...args);
×
390
    return this;
1✔
391
  },
1✔
392
  fetch(...args) {
1✔
393
    log.warn('fetch method is deprecated, use $fetch instead!');
1✔
394
    return this.$fetch(...args);
1✔
395
  },
1✔
396

1✔
397
  /**
1✔
398
   * Adds or updates a model on the server.
1✔
399
   * (public method - must not be overwritten)
1✔
400
   *
1✔
401
   * Nota:
1✔
402
   * Fires a 'save' event if not silent ({ silent: true }).
1✔
403
   *
1✔
404
   * @method (arg1, [arg2], [arg3])
1✔
405
   * @public
1✔
406
   * @param {Object}        the updated attributes,
1✔
407
   * @param {Object}        the options,
10✔
408
   * @param {Function}      the function to call at the completion,
1✔
409
   * @returns {Object}      this,
×
410
   * @since 0.0.0
×
411
   */
×
412
  $save(...args) {
1✔
413
    F.save(this, this.url, ...args);
1✔
414
    return this;
1✔
415
  },
1✔
416
  save(...args) {
1✔
417
    log.warn('save method is deprecated, use $save instead!');
1✔
418
    return this.$save(...args);
1✔
419
  },
1✔
420

1✔
421
  /**
1✔
422
   * Removes a model from the server.
1✔
423
   * (public method - must not be overwritten)
1✔
424
   *
1✔
425
   * Nota:
1✔
426
   * Fires a 'delete' event if not silent ({ silent: true }).
1✔
427
   *
1✔
428
   * @method ([arg1], [arg2])
1✔
429
   * @public
1✔
430
   * @param {Object}        the options,
1✔
431
   * @param {Function}      the function to call at the completion,
1✔
432
   * @returns {Object}      this,
1✔
433
   * @since 0.0.0
×
434
   */
×
435
  $delete(...args) {
×
436
    F.delete(this, this.url, ...args);
×
437
    return this;
1✔
438
  },
1✔
439
  delete(...args) {
1✔
440
    log.warn('delete method is deprecated, use $delete instead!');
1✔
441
    return this.$delete(...args);
1✔
442
  },
1✔
443

1✔
444
  /**
1✔
445
   * Extends url with query parameters,
1✔
446
   *
1✔
447
   * @method (arg1, arg2)
1✔
448
   * @public
1✔
449
   * @param {String}        the server url,
1✔
450
   * @param {Object}        the query parameters,
1✔
451
   * @returns {String}      returns the URI or null,
1✔
452
   * @since 0.0.0
1✔
453
   */
8✔
454
  $urify(...args) {
8✔
455
    return U.urify(...args);
8✔
456
  },
8✔
457
  urify(...args) {
1✔
458
    log.warn('urify method is deprecated, use $urify instead!');
×
459
    return this.$urify(...args);
×
460
  },
×
461
};
×
462

×
463

×
464
// -- Export
×
465
export default { Model };
1✔
466

1✔
467
/* eslint-enable one-var, semi-style, no-underscore-dangle */
1✔
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

© 2026 Coveralls, Inc