From d9511d817a3cb10127d7eb88f3b5d42e1c3d4ae8 Mon Sep 17 00:00:00 2001 From: James Ide Date: Fri, 16 Aug 2013 14:52:20 -0700 Subject: [PATCH] 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. --- src/utils/__tests__/objFilter-test.js | 88 -------------------- src/utils/__tests__/objMap-test.js | 113 -------------------------- src/utils/bindNoArgs.js | 30 ------- src/utils/curryOnly.js | 37 --------- src/utils/eachKeyVal.js | 39 --------- src/utils/objFilter.js | 48 ----------- src/utils/objMap.js | 47 ----------- 7 files changed, 402 deletions(-) delete mode 100644 src/utils/__tests__/objFilter-test.js delete mode 100644 src/utils/__tests__/objMap-test.js delete mode 100644 src/utils/bindNoArgs.js delete mode 100644 src/utils/curryOnly.js delete mode 100644 src/utils/eachKeyVal.js delete mode 100644 src/utils/objFilter.js delete mode 100644 src/utils/objMap.js diff --git a/src/utils/__tests__/objFilter-test.js b/src/utils/__tests__/objFilter-test.js deleted file mode 100644 index 458594f1ae..0000000000 --- a/src/utils/__tests__/objFilter-test.js +++ /dev/null @@ -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' - }); - }); - -}); diff --git a/src/utils/__tests__/objMap-test.js b/src/utils/__tests__/objMap-test.js deleted file mode 100644 index 0c6ee93ba3..0000000000 --- a/src/utils/__tests__/objMap-test.js +++ /dev/null @@ -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 - }); - }); - -}); diff --git a/src/utils/bindNoArgs.js b/src/utils/bindNoArgs.js deleted file mode 100644 index 720b25cacb..0000000000 --- a/src/utils/bindNoArgs.js +++ /dev/null @@ -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; diff --git a/src/utils/curryOnly.js b/src/utils/curryOnly.js deleted file mode 100644 index 3b2183f26d..0000000000 --- a/src/utils/curryOnly.js +++ /dev/null @@ -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; diff --git a/src/utils/eachKeyVal.js b/src/utils/eachKeyVal.js deleted file mode 100644 index ee21665ee5..0000000000 --- a/src/utils/eachKeyVal.js +++ /dev/null @@ -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; diff --git a/src/utils/objFilter.js b/src/utils/objFilter.js deleted file mode 100644 index 65ad395119..0000000000 --- a/src/utils/objFilter.js +++ /dev/null @@ -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; diff --git a/src/utils/objMap.js b/src/utils/objMap.js deleted file mode 100644 index b477c9290f..0000000000 --- a/src/utils/objMap.js +++ /dev/null @@ -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;