-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
184 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const intersection = require('../intersection'); | ||
|
||
function test(testCases) { | ||
for (const testName in testCases) { | ||
const testCase = testCases[testName]; | ||
it(testName, function() { | ||
expect(intersection(testCase.input)).toEqual(testCase.output); | ||
}); | ||
} | ||
} | ||
|
||
describe('intersection', function() { | ||
test({ | ||
'intersects string values': { | ||
input: [['foo', 'bar', 'baz'], ['foo', 'bar'], ['bar', 'baz']], | ||
output: ['bar'], | ||
}, | ||
|
||
'returns empty list if no intersection': { | ||
input: [['foo', 'bar', 'baz'], ['foo'], ['bar']], | ||
output: [], | ||
}, | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const once = require('../once'); | ||
|
||
describe('once', function() { | ||
it('executes the function only once', function() { | ||
const mock = jest.fn().mockImplementation(foo => foo); | ||
const wrapped = once(mock); | ||
|
||
wrapped('foo'); | ||
const result = wrapped('bar'); | ||
|
||
expect(result).toEqual('foo'); | ||
expect(mock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const union = require('../union'); | ||
|
||
function test(testCases) { | ||
for (const testName in testCases) { | ||
const testCase = testCases[testName]; | ||
it(testName, function() { | ||
expect(union(testCase.input)).toEqual(testCase.output); | ||
}); | ||
} | ||
} | ||
|
||
describe('union', function() { | ||
test({ | ||
'unions string values': { | ||
input: [['foo', 'bar', 'baz'], ['foo', 'bar'], ['bar', 'baz']], | ||
output: ['foo', 'bar', 'baz'], | ||
}, | ||
|
||
'understands empty input arrays': { | ||
input: [[], ['foo'], ['bar']], | ||
output: ['foo', 'bar'], | ||
}, | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
|
||
module.exports = function(arrays) { | ||
const result = new Set(arrays[0]); | ||
let resultSize = result.length; | ||
|
||
let i, value, valuesToCheck; | ||
for (i = 1; i < arrays.length; i++) { | ||
valuesToCheck = new Set(arrays[i]); | ||
for (value of result) { | ||
if (!valuesToCheck.has(value)) { | ||
result.delete(value); | ||
resultSize -= 1; | ||
} | ||
if (resultSize === 0) { | ||
return []; | ||
} | ||
} | ||
} | ||
|
||
return Array.from(result); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
|
||
/** | ||
* This replicates lodash's once functionality for our purposes. | ||
*/ | ||
module.exports = function(func) { | ||
let called = false; | ||
let result; | ||
return function(...args) { | ||
if (called) { | ||
return result; | ||
} | ||
called = true; | ||
return result = func.apply(this, args); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
|
||
module.exports = function(arrays) { | ||
const result = new Set(arrays[0]); | ||
|
||
let i,j, array; | ||
for (i = 1; i < arrays.length; i++) { | ||
array = arrays[i]; | ||
for (j = 0; j < array.length; j++) { | ||
result.add(array[j]); | ||
} | ||
} | ||
|
||
return Array.from(result); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters