ran-boilerplate
Version:
React . Apollo (GraphQL) . Next.js Toolkit
136 lines (134 loc) • 3.71 kB
JavaScript
;
/**
* Copyright 2017 Google 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
// See http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/
exports.contains = function (obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
};
exports.safeGet = function (obj, key) {
if (Object.prototype.hasOwnProperty.call(obj, key))
return obj[key];
// else return undefined.
};
/**
* Enumerates the keys/values in an object, excluding keys defined on the prototype.
*
* @param {?Object.<K,V>} obj Object to enumerate.
* @param {!function(K, V)} fn Function to call for each key and value.
* @template K,V
*/
exports.forEach = function (obj, fn) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
fn(key, obj[key]);
}
}
};
/**
* Copies all the (own) properties from one object to another.
* @param {!Object} objTo
* @param {!Object} objFrom
* @return {!Object} objTo
*/
exports.extend = function (objTo, objFrom) {
exports.forEach(objFrom, function (key, value) {
objTo[key] = value;
});
return objTo;
};
/**
* Returns a clone of the specified object.
* @param {!Object} obj
* @return {!Object} cloned obj.
*/
exports.clone = function (obj) {
return exports.extend({}, obj);
};
/**
* Returns true if obj has typeof "object" and is not null. Unlike goog.isObject(), does not return true
* for functions.
*
* @param obj {*} A potential object.
* @returns {boolean} True if it's an object.
*/
exports.isNonNullObject = function (obj) {
return typeof obj === 'object' && obj !== null;
};
exports.isEmpty = function (obj) {
for (var key in obj) {
return false;
}
return true;
};
exports.getCount = function (obj) {
var rv = 0;
for (var key in obj) {
rv++;
}
return rv;
};
exports.map = function (obj, f, opt_obj) {
var res = {};
for (var key in obj) {
res[key] = f.call(opt_obj, obj[key], key, obj);
}
return res;
};
exports.findKey = function (obj, fn, opt_this) {
for (var key in obj) {
if (fn.call(opt_this, obj[key], key, obj)) {
return key;
}
}
return undefined;
};
exports.findValue = function (obj, fn, opt_this) {
var key = exports.findKey(obj, fn, opt_this);
return key && obj[key];
};
exports.getAnyKey = function (obj) {
for (var key in obj) {
return key;
}
};
exports.getValues = function (obj) {
var res = [];
var i = 0;
for (var key in obj) {
res[i++] = obj[key];
}
return res;
};
/**
* Tests whether every key/value pair in an object pass the test implemented
* by the provided function
*
* @param {?Object.<K,V>} obj Object to test.
* @param {!function(K, V)} fn Function to call for each key and value.
* @template K,V
*/
exports.every = function (obj, fn) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (!fn(key, obj[key])) {
return false;
}
}
}
return true;
};
//# sourceMappingURL=obj.js.map