mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
64 lines
1.7 KiB
JavaScript
64 lines
1.7 KiB
JavaScript
/**
|
|
* 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 createObjectFrom
|
|
*/
|
|
|
|
var hasArrayNature = require("./hasArrayNature");
|
|
|
|
/**
|
|
* Construct an object from an array of keys
|
|
* and optionally specified value or list of values.
|
|
*
|
|
* >>> createObjectFrom(['a','b','c']);
|
|
* {a: true, b: true, c: true}
|
|
*
|
|
* >>> createObjectFrom(['a','b','c'], false);
|
|
* {a: false, b: false, c: false}
|
|
*
|
|
* >>> createObjectFrom(['a','b','c'], 'monkey');
|
|
* {c:'monkey', b:'monkey' c:'monkey'}
|
|
*
|
|
* >>> createObjectFrom(['a','b','c'], [1,2,3]);
|
|
* {a: 1, b: 2, c: 3}
|
|
*
|
|
* >>> createObjectFrom(['women', 'men'], [true, false]);
|
|
* {women: true, men: false}
|
|
*
|
|
* @param Array list of keys
|
|
* @param mixed optional value or value array. defaults true.
|
|
* @returns object
|
|
*/
|
|
function createObjectFrom(keys, values /* = true */) {
|
|
if (true) {
|
|
if (!hasArrayNature(keys)) {
|
|
throw new TypeError('Must pass an array of keys.');
|
|
}
|
|
}
|
|
|
|
var object = {};
|
|
var is_array = hasArrayNature(values);
|
|
if (typeof values == 'undefined') {
|
|
values = true;
|
|
}
|
|
|
|
for (var ii = keys.length; ii--;) {
|
|
object[keys[ii]] = is_array ? values[ii] : values;
|
|
}
|
|
return object;
|
|
}
|
|
|
|
module.exports = createObjectFrom;
|