UNPKG

2.83 kBJavaScriptView Raw
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2015 - present Instructure, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25/**
26 * ---
27 * category: utilities/react
28 * ---
29 * Return an object with the remaining props after the given props are omitted.
30 *
31 * Automatically excludes the following props:
32 * 'theme', 'children', 'className', 'style', 'styles', 'makeStyles', 'themeOverride', 'deterministicId'
33 * @module omitProps
34 * @param props The object to process
35 * @param propsToOmit list disallowed prop keys or an object whose
36 * keys will be omitted. If not specified the automatically excluded
37 * props will be removed.
38 * @param exclude an optional array of disallowed prop names to omit
39 * @returns props object without the excluded props
40 * @module omitProps
41 */
42function omitProps(props, propsToOmit, exclude) {
43 const propKeys = Array.isArray(propsToOmit) ? propsToOmit : Object.keys(propsToOmit || {});
44 const keysToOmit = exclude ? propKeys.concat(exclude) : propKeys;
45 return omit(props, keysToOmit);
46}
47
48const hasOwnProperty = Object.prototype.hasOwnProperty;
49
50const omit = (originalObject, keysToOmit) => {
51 // code based on babel's _objectWithoutProperties
52 const newObject = {};
53
54 for (const key in originalObject) {
55 // special case because we always want to omit these and === is faster than
56 // concat'ing them in
57 if (key === 'theme' || key === 'children' || key === 'className' || key === 'style' || key === 'styles' || key === 'makeStyles' || key === 'themeOverride' || key === 'deterministicId') {
58 continue;
59 }
60
61 if (keysToOmit.includes(key) || !hasOwnProperty.call(originalObject, key)) {
62 continue;
63 }
64
65 newObject[key] = originalObject[key];
66 }
67
68 return newObject;
69};
70
71export default omitProps;
72export { omitProps };
\No newline at end of file