src/utils.js   A
last analyzed

Complexity

Total Complexity 14
Complexity/F 2.33

Size

Lines of Code 87
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 0
c 4
b 0
f 0
nc 1
dl 0
loc 87
ccs 16
cts 18
cp 0.8889
crap 0
rs 10
wmc 14
mnd 2
bc 13
fnc 6
bpm 2.1666
cpm 2.3333
noi 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A utils.js ➔ noop 0 2 1
A utils.js ➔ warn 0 6 3
A utils.js ➔ isNumericIDLike 0 3 1
A utils.js ➔ pick 0 7 2
A utils.js ➔ isObject 0 3 1
B utils.js ➔ looseEqual 0 16 6
1
import * as feathersUtil from 'feathers-commons/lib/utils'
2
3 9
export const each = feathersUtil.each
4 9
export const some = feathersUtil._.some
5
6
/**
7
 * Empty function
8
 */
9
export function noop() {
10
}
11
12
/**
13
 * Log debug in user's console
14
 *
15
 * @param args
16
 */
17
export function warn(...args) {
18
	/* istanbul ignore next */
19
	if (console || window.console) {
20
		console.warn('[vue-syncers-feathers]', ...args)
21
	}
22
}
23
24 9
const numberRegex = /^\d+$/
25
26
/**
27
 * Test if a value seems like a number
28
 *
29
 * @param value
30
 * @returns {boolean}
31
 */
32
33
export function isNumericIDLike(value) {
34 22
	return (typeof value !== 'number' && numberRegex.test(value))
35
}
36
37
/**
38
 * Return object with only selected keys
39
 *
40
 * @from https://github.com/feathersjs/feathers-memory
41
 * @param source
42
 * @param keys
43
 * @returns {object}
44
 */
45
export function pick(source, ...keys) {
46 3
	const result = {}
47 3
	for (const key of keys) {
48 6
		result[key] = source[key]
49
	}
50 3
	return result
51
}
52
53
/**
54
 * Check if object is JSONable
55
 *
56
 * @from https://github.com/vuejs/vue/blob/0b902e0c28f4f324ffb8efbc9db74127430f8a42/src/shared/util.js#L155
57
 * @param {*} obj
58
 * @returns {boolean}
59
 */
60
function isObject(obj) {
61 24
	return obj !== null && typeof obj === 'object'
62
}
63
64
/**
65
 * Loosely check if objects are equal
66
 *
67
 * @from https://github.com/vuejs/vue/blob/0b902e0c28f4f324ffb8efbc9db74127430f8a42/src/shared/util.js
68
 * @param {*} a
69
 * @param {*} b
70
 * @returns {boolean}
71
 */
72
export function looseEqual(a, b) {
73 12
	const isObjectA = isObject(a)
74 12
	const isObjectB = isObject(b)
75 12
	if (isObjectA && isObjectB) {
76 1
		try {
77 1
			return JSON.stringify(a) === JSON.stringify(b)
78
		} catch (err) {
79
			// Possible circular reference
80
			return a === b
81
		}
82 11
	} else if (!isObjectA && !isObjectB) {
83
		return String(a) === String(b)
84
	} else {
85 11
		return false
86
	}
87
}
88