-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (39 loc) · 960 Bytes
/
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
/*!
* longest-value <https://github.com/jonschlinkert/longest-value>
*
* Copyright (c) 2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var longest = require('longest');
module.exports = function longestValue(obj, prop) {
if (typeof obj !== 'object') {
throw new TypeError('longest-value expects an object or array.');
}
if (Array.isArray(obj)) {
return longest(arrayValues(obj, prop));
}
return longest(objectValues(obj));
};
function objectValues(obj) {
var values = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
values.push(obj[key]);
}
}
return values;
}
function arrayValues(arr, prop) {
var values = [];
var len = arr.length;
while (len--) {
var obj = arr[len];
if (obj.hasOwnProperty(prop)) {
values.push(arr[len][prop]);
} else {
throw new Error('longest-value cannot find ' + prop + ' in array objects.');
}
}
return values;
}