-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
69 lines (55 loc) · 2.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
const devices = require( './devices.json' );
const brands = require( './brands.json' );
function deviceList () {
return devices;
}
function brandList () {
return brands;
}
function getDevices ( find, field, caseInsensitive, contains ) {
return devices.filter( device => {
return device[field] === find ||
contains && device[field].indexOf( find ) !== -1 ||
caseInsensitive && device[field].toLowerCase() === find.toLowerCase() ||
caseInsensitive && contains && device[field].toLowerCase().indexOf( find.toLowerCase() ) !== -1;
} );
}
function getDevicesByBrand ( brand, options ) {
if ( typeof brand !== 'string' ) {
throw new TypeError( '`brand` parameter must be a string' );
}
options = options || {};
let caseInsensitive = !!options.caseInsensitive;
let contains = !!options.contains;
return getDevices( brand, 'brand', caseInsensitive, contains );
}
function getDevicesByName ( name, options ) {
if ( typeof name !== 'string' ) {
throw new TypeError( '`name` parameter must be a string' );
}
options = options || {};
let caseInsensitive = !!options.caseInsensitive;
let contains = !!options.contains;
return getDevices( name, 'name', caseInsensitive, contains );
}
function getDevicesByDeviceId ( deviceId, options ) {
if ( typeof deviceId !== 'string' ) {
throw new TypeError( '`deviceId` parameter must be a string' );
}
options = options || {};
let caseInsensitive = !!options.caseInsensitive;
let contains = !!options.contains;
return getDevices( deviceId, 'device', caseInsensitive, contains );
}
function getDevicesByModel ( model, options ) {
if ( typeof model !== 'string' ) {
throw new TypeError( '`model` parameter must be a string' );
}
options = options || {};
let caseInsensitive = !!options.caseInsensitive;
let contains = !!options.contains;
return getDevices( model, 'model', caseInsensitive, contains );
}
module.exports = { deviceList, brandList,
getDevicesByBrand, getDevicesByName, getDevicesByDeviceId, getDevicesByModel };