mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Move utils out of React that aren't being used
Many of React's util functions are non-redundant with Facebook's core libraries, so move them out of React into a central location (out of this repo). These files were not getting used by any part of React core, so didn't actually belong here anyway.
This commit is contained in:
committed by
Paul O’Shannessy
parent
2fda70fb4a
commit
d9511d817a
@@ -1,88 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* @emails react-core
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
var objFilter = require('objFilter');
|
||||
|
||||
describe('objFilter', function() {
|
||||
|
||||
var obj = {
|
||||
A: 'apple',
|
||||
B: 'banana',
|
||||
C: 'coconut',
|
||||
D: 'durian',
|
||||
E: 'elderberry',
|
||||
F: 'fig',
|
||||
G: 'guava',
|
||||
H: 'hackberry'
|
||||
};
|
||||
|
||||
it('should accept null', function() {
|
||||
var filtered = objFilter(null, function() {});
|
||||
expect(filtered).toBe(null);
|
||||
});
|
||||
|
||||
it('should return true to create copy', function() {
|
||||
var filtered = objFilter(obj, function() {
|
||||
return true;
|
||||
});
|
||||
expect(filtered).not.toBe(obj);
|
||||
expect(filtered).toEqual(obj);
|
||||
});
|
||||
|
||||
it('should return empty object for a falsey func', function() {
|
||||
var filtered = objFilter(obj, function() {
|
||||
return false;
|
||||
});
|
||||
expect(filtered).toEqual({});
|
||||
});
|
||||
|
||||
it('should filter based on value', function() {
|
||||
var filtered = objFilter(obj, function(value) {
|
||||
return value.indexOf('berry') !== -1;
|
||||
});
|
||||
expect(filtered).toEqual({
|
||||
E: 'elderberry',
|
||||
H: 'hackberry'
|
||||
});
|
||||
});
|
||||
|
||||
it('should filter based on key', function() {
|
||||
var filtered = objFilter(obj, function(value, key) {
|
||||
return (/[AEIOU]/).test(key);
|
||||
});
|
||||
expect(filtered).toEqual({
|
||||
A: 'apple',
|
||||
E: 'elderberry'
|
||||
});
|
||||
});
|
||||
|
||||
it('should filter based on iteration', function() {
|
||||
var filtered = objFilter(obj, function(value, key, iteration) {
|
||||
return iteration % 2;
|
||||
});
|
||||
expect(filtered).toEqual({
|
||||
B: 'banana',
|
||||
D: 'durian',
|
||||
F: 'fig',
|
||||
H: 'hackberry'
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,113 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* @emails react-core
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
var objMap = require('objMap');
|
||||
|
||||
describe('objMap', function() {
|
||||
|
||||
var obj = {
|
||||
A: 'apple',
|
||||
B: 'banana',
|
||||
C: 'coconut',
|
||||
D: 'durian',
|
||||
E: 'elderberry',
|
||||
F: 'fig',
|
||||
G: 'guava',
|
||||
H: 'hackberry'
|
||||
};
|
||||
|
||||
it('should accept null', function() {
|
||||
var mapped = objMap(null, function() {});
|
||||
expect(mapped).toBe(null);
|
||||
});
|
||||
|
||||
it('should return value to create copy', function() {
|
||||
var mapped = objMap(obj, function(value) {
|
||||
return value;
|
||||
});
|
||||
expect(mapped).not.toBe(obj);
|
||||
expect(mapped).toEqual(obj);
|
||||
});
|
||||
|
||||
it('should always retain keys', function() {
|
||||
var mapped = objMap(obj, function() {
|
||||
return null;
|
||||
});
|
||||
expect(mapped).toEqual({
|
||||
A: null,
|
||||
B: null,
|
||||
C: null,
|
||||
D: null,
|
||||
E: null,
|
||||
F: null,
|
||||
G: null,
|
||||
H: null
|
||||
});
|
||||
});
|
||||
|
||||
it('should map values', function() {
|
||||
var mapped = objMap(obj, function (value) {
|
||||
return value.toUpperCase();
|
||||
});
|
||||
expect(mapped).toEqual({
|
||||
A: 'APPLE',
|
||||
B: 'BANANA',
|
||||
C: 'COCONUT',
|
||||
D: 'DURIAN',
|
||||
E: 'ELDERBERRY',
|
||||
F: 'FIG',
|
||||
G: 'GUAVA',
|
||||
H: 'HACKBERRY'
|
||||
});
|
||||
});
|
||||
|
||||
it('should map keys', function() {
|
||||
var mapped = objMap(obj, function (value, key) {
|
||||
return key;
|
||||
});
|
||||
expect(mapped).toEqual({
|
||||
A: 'A',
|
||||
B: 'B',
|
||||
C: 'C',
|
||||
D: 'D',
|
||||
E: 'E',
|
||||
F: 'F',
|
||||
G: 'G',
|
||||
H: 'H'
|
||||
});
|
||||
});
|
||||
|
||||
it('should map iterations', function() {
|
||||
var mapped = objMap(obj, function (value, key, iteration) {
|
||||
return iteration;
|
||||
});
|
||||
expect(mapped).toEqual({
|
||||
A: 0,
|
||||
B: 1,
|
||||
C: 2,
|
||||
D: 3,
|
||||
E: 4,
|
||||
F: 5,
|
||||
G: 6,
|
||||
H: 7
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,30 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* @providesModule bindNoArgs
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
var bindNoArgs = function (func, context) {
|
||||
if (!func) {
|
||||
return null;
|
||||
}
|
||||
return function () {
|
||||
return func.call(context);
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = bindNoArgs;
|
||||
@@ -1,37 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* @providesModule curryOnly
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* When the function who's first parameter you are currying accepts only a
|
||||
* single argument, and you want to curry it, use this function for performance
|
||||
* reasons, as it will never access 'arguments'. It would be an interesting
|
||||
* project to detect at static analysis time, calls to F.curry that could be
|
||||
* transformed to one of the two optimized versions seen here.
|
||||
*/
|
||||
var curryOnly = function(func, val, context) {
|
||||
if (!func) {
|
||||
return null;
|
||||
}
|
||||
return function() {
|
||||
return func.call(context, val);
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = curryOnly;
|
||||
@@ -1,39 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* @providesModule eachKeyVal
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Invokes fun for each own property in obj. Invokes `fun(key, val, obj, i)`.
|
||||
* @param {?Object} obj The object to iterate over.
|
||||
* @param {?Function} fun The function to invoke.
|
||||
* @param {?context=} context The context to call from.
|
||||
*/
|
||||
function eachKeyVal(obj, fun, context) {
|
||||
if (!obj || !fun) {
|
||||
return;
|
||||
}
|
||||
// Object.keys only returns the "own" properties.
|
||||
var objKeys = Object.keys(obj);
|
||||
var i;
|
||||
for (i=0; i < objKeys.length; i++) {
|
||||
fun.call(context, objKeys[i], obj[objKeys[i]], obj, i);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = eachKeyVal;
|
||||
@@ -1,48 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* @providesModule objFilter
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* For each key/value pair, invokes callback func and constructs a resulting
|
||||
* object which contains each key/value pair which produces a truthy result
|
||||
* from invoking the function:
|
||||
*
|
||||
* func(value, key, iteration)
|
||||
*
|
||||
* @param {?object} obj Object to map keys over
|
||||
* @param {function} func Invoked for each key/val pair.
|
||||
* @param {?*} context
|
||||
* @return {?object} Result of filtering or null if obj is falsey
|
||||
*/
|
||||
function objFilter(obj, func, context) {
|
||||
if (!obj) {
|
||||
return null;
|
||||
}
|
||||
var i = 0;
|
||||
var ret = {};
|
||||
for (var key in obj) {
|
||||
if (obj.hasOwnProperty(key) &&
|
||||
func.call(context, obj[key], key, i++)) {
|
||||
ret[key] = obj[key];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
module.exports = objFilter;
|
||||
@@ -1,47 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* @providesModule objMap
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* For each key/value pair, invokes callback func and constructs a resulting
|
||||
* object which contains, for every key in obj, values that are the result of
|
||||
* of invoking the function:
|
||||
*
|
||||
* func(value, key, iteration)
|
||||
*
|
||||
* @param {?object} obj Object to map keys over
|
||||
* @param {function} func Invoked for each key/val pair.
|
||||
* @param {?*} context
|
||||
* @return {?object} Result of mapping or null if obj is falsey
|
||||
*/
|
||||
function objMap(obj, func, context) {
|
||||
if (!obj) {
|
||||
return null;
|
||||
}
|
||||
var i = 0;
|
||||
var ret = {};
|
||||
for (var key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
ret[key] = func.call(context, obj[key], key, i++);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
module.exports = objMap;
|
||||
Reference in New Issue
Block a user