Skip to content

Commit 16db7e6

Browse files
committed
fix: improve handling of bindAll to work with objects that have no prototype
1 parent 4326059 commit 16db7e6

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

spec/fauxdash.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ describe('fauxdash', function () {
157157
})
158158

159159
it('should parse function correctly', function () {
160-
return _.parseFunction(function one (a, b, c) {})
161-
.should.partiallyEql({ name: 'one', arguments: ['a', 'b', 'c'] })
160+
var results = _.parseFunction(function one (a, b, c) {})
161+
console.log(results)
162+
return results.should.partiallyEql({ name: 'one', arguments: ['a', 'b', 'c'] })
162163
})
163164

164165
it('should parse function correctly', function () {

spec/setup.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ global.should = chai.should()
55
global.expect = chai.expect
66

77
function deepCompare (a, b, k) {
8-
var diffs = []
8+
let diffs = []
99
if (b === undefined && a !== undefined) {
1010
diffs.push('expected ' + k + ' to equal ' + a + ' but was undefined ')
1111
} else if (_.isObject(a) || Array.isArray(a)) {
1212
_.each(a, function (v, c) {
13-
var key = k ? [ k, c ].join('.') : c
14-
diffs = diffs.concat(deepCompare(a[ c ], b[ c ], key))
13+
const key = k ? [k, c].join('.') : c
14+
diffs = diffs.concat(deepCompare(a[c], b[c], key))
1515
})
1616
} else {
1717
var equal = a == b // eslint-disable-line
@@ -23,16 +23,16 @@ function deepCompare (a, b, k) {
2323
}
2424

2525
chai.Assertion.addMethod('partiallyEql', function (partial) {
26-
var obj = this._obj
26+
let obj = this._obj
2727
if (!obj.then) {
2828
obj = Promise.resolve(obj)
2929
}
30-
var self = this
30+
const self = this
3131
return obj.then(function (actual) {
32-
var diffs = deepCompare(partial, actual)
32+
const diffs = deepCompare(partial, actual)
3333
return self.assert(
34-
diffs.length === 0,
35-
diffs.join('\n\t')
34+
diffs.length === 0,
35+
diffs.join('\n\t')
3636
)
3737
})
3838
})

src/index.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ function applyWhen (fn, args) {
6767
}
6868

6969
function bindAll (obj) {
70-
const names = Object.getOwnPropertyNames(obj.prototype)
70+
const target = obj.prototype !== undefined ? obj : obj.prototype
71+
const names = Object.getOwnPropertyNames(target)
7172
names.forEach(name => {
72-
const prop = obj.prototype[ name ]
73+
const prop = target[name]
7374
if (typeof prop === 'function') {
74-
obj[ prop ] = obj.prop.bind(obj)
75+
obj[prop] = obj.prop.bind(obj)
7576
}
7677
})
7778
}
@@ -90,8 +91,8 @@ function clone (source, target) {
9091
}
9192

9293
target = target || new source.constructor()
93-
for (var key in source) {
94-
target[ key ] = typeof target[ key ] === 'undefined' ? clone(source[ key ], null) : target[ key ]
94+
for (const key in source) {
95+
target[key] = typeof target[key] === 'undefined' ? clone(source[key], null) : target[key]
9596
}
9697
return target
9798
}
@@ -102,9 +103,9 @@ function contains (list, value) {
102103

103104
function defaults (target, ...sources) {
104105
sources.forEach((source) => {
105-
for (let key in source) {
106-
if (!target[ key ]) {
107-
target[ key ] = source[ key ]
106+
for (const key in source) {
107+
if (!target[key]) {
108+
target[key] = source[key]
108109
}
109110
}
110111
})
@@ -130,9 +131,9 @@ function find (list, predicate = y => y) {
130131
}
131132
let found = false
132133
let index = -1
133-
var item
134+
let item
134135
do {
135-
item = list[ ++index ]
136+
item = list[++index]
136137
found = predicate(item)
137138
} while (!found && index < list.length - 1)
138139
return found ? item : undefined
@@ -147,7 +148,7 @@ function flatten (list) {
147148
function getArguments (fn) {
148149
const source = fn.toString().replace(NYC_DEFAULT_REGEX, '')
149150
const match = ARGUMENT_REGEX.exec(source)
150-
return filter(match[ 2 ].replace(/[) ]/g, '').split(','))
151+
return filter(match[2].replace(/[) ]/g, '').split(','))
151152
.map(x => x.split('=')[0])
152153
}
153154

@@ -159,7 +160,7 @@ function getObjectTag (value) {
159160
}
160161

161162
function has (object, key) {
162-
return object && exists(object[ key ])
163+
return object && exists(object[key])
163164
}
164165

165166
function intersection (a, b) {
@@ -246,23 +247,23 @@ function map (obj, fn) {
246247
}
247248

248249
function mapCall (method, map) {
249-
let argumentList = getArguments(method).slice(1)
250+
const argumentList = getArguments(method).slice(1)
250251
if (map === false) {
251252
return method
252253
} else if (map) {
253254
return function (actor, message) {
254-
let appliedArgs = [ actor ]
255+
const appliedArgs = [actor]
255256
argumentList.forEach((arg) => {
256-
let prop = map[ arg ] ? map[ arg ] : arg
257-
appliedArgs.push(message[ prop ])
257+
const prop = map[arg] ? map[arg] : arg
258+
appliedArgs.push(message[prop])
258259
})
259260
return method.apply(undefined, appliedArgs)
260261
}
261262
} else {
262263
return function (actor, message) {
263-
let appliedArgs = [ actor ]
264+
const appliedArgs = [actor]
264265
argumentList.forEach((arg) => {
265-
appliedArgs.push(message[ arg ])
266+
appliedArgs.push(message[arg])
266267
})
267268
return method.apply(undefined, appliedArgs)
268269
}
@@ -289,12 +290,12 @@ function parseFunction (fn) {
289290
const source = fn.toString().replace(NYC_DEFAULT_REGEX, '')
290291
const parts = FUNCTION_REGEX.exec(source)
291292
return {
292-
name: parts[ 2 ] ? parts[ 2 ].trim() : undefined,
293-
arguments: filter(parts[ 3 ]
294-
.replace(/\s/g, '')
293+
name: parts[2] ? parts[2].trim() : undefined,
294+
arguments: filter(parts[3]
295+
.replace(/(\s|[\(])/g, '')
295296
.split(','))
296297
.map(x => x.split('=')[0]),
297-
body: parts[ 4 ]
298+
body: parts[4]
298299
}
299300
}
300301

@@ -310,18 +311,18 @@ function sequence (...args) {
310311
if (!calls || calls.length === 0) {
311312
return Promise.resolve([])
312313
}
313-
let list = new Array(calls.length)
314+
const list = new Array(calls.length)
314315
let index = -1
315316
function invoke () {
316-
const value = calls[ ++index ]()
317+
const value = calls[++index]()
317318
if (isPromisey(value)) {
318319
return value.then(next, next)
319320
} else {
320321
return next(value)
321322
}
322323
}
323324
function next (result) {
324-
list[ index ] = result
325+
list[index] = result
325326
if (index < list.length - 1) {
326327
return invoke()
327328
} else {
@@ -333,9 +334,9 @@ function sequence (...args) {
333334

334335
function sortBy (list, prop) {
335336
list.sort((a, b) => {
336-
if (a[ prop ] < b[ prop ]) {
337+
if (a[prop] < b[prop]) {
337338
return -1
338-
} else if (a[ prop ] > b[ prop ]) {
339+
} else if (a[prop] > b[prop]) {
339340
return 1
340341
} else {
341342
return 0
@@ -348,7 +349,7 @@ function transform (obj, aliases, ...omit) {
348349
const list = flatten(omit)
349350
return reduce(obj, (o, v, k) => {
350351
if (!contains(list, k)) {
351-
o[ aliases[k] || k ] = v
352+
o[aliases[k] || k] = v
352353
}
353354
return o
354355
}, {})
@@ -376,9 +377,9 @@ function uniq (original) {
376377
}
377378

378379
function unique (list, identity = x => x) {
379-
let ids = []
380+
const ids = []
380381
return list.reduce((acc, item) => {
381-
let id = identity(item)
382+
const id = identity(item)
382383
if (ids.indexOf(id) < 0) {
383384
ids.push(id)
384385
acc.push(item)

0 commit comments

Comments
 (0)