bootstrap-vue
Version:
With more than 85 components, over 45 available plugins, several directives, and 1000+ icons, BootstrapVue provides one of the most comprehensive implementations of the Bootstrap v4 component and grid system available for Vue.js v2.6, complete with extens
94 lines (82 loc) • 3.42 kB
JavaScript
import { isArray, isObject, isPlainObject } from './inspect'
// --- Static ---
export const assign = (...args) => Object.assign(...args)
export const create = (proto, optionalProps) => Object.create(proto, optionalProps)
export const defineProperties = (obj, props) => Object.defineProperties(obj, props)
export const defineProperty = (obj, prop, descriptor) =>
Object.defineProperty(obj, prop, descriptor)
export const freeze = obj => Object.freeze(obj)
export const getOwnPropertyNames = obj => Object.getOwnPropertyNames(obj)
export const getOwnPropertyDescriptor = (obj, prop) => Object.getOwnPropertyDescriptor(obj, prop)
export const getOwnPropertySymbols = obj => Object.getOwnPropertySymbols(obj)
export const getPrototypeOf = obj => Object.getPrototypeOf(obj)
export const is = (value1, value2) => Object.is(value1, value2)
export const isFrozen = obj => Object.isFrozen(obj)
export const keys = obj => Object.keys(obj)
// --- "Instance" ---
export const hasOwnProperty = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
export const toString = obj => Object.prototype.toString.call(obj)
// --- Utilities ---
/**
* Shallow copy an object. If the passed in object
* is null or undefined, returns an empty object
*/
export const clone = obj => ({ ...obj })
/**
* Return a shallow copy of object with the specified properties only
* @link https://gist.github.com/bisubus/2da8af7e801ffd813fab7ac221aa7afc
*/
export const pick = (obj, props) =>
keys(obj)
.filter(key => props.indexOf(key) !== -1)
.reduce((result, key) => ({ ...result, [key]: obj[key] }), {})
/**
* Return a shallow copy of object with the specified properties omitted
* @link https://gist.github.com/bisubus/2da8af7e801ffd813fab7ac221aa7afc
*/
export const omit = (obj, props) =>
keys(obj)
.filter(key => props.indexOf(key) === -1)
.reduce((result, key) => ({ ...result, [key]: obj[key] }), {})
/**
* Merges two object deeply together
* @link https://gist.github.com/Salakar/1d7137de9cb8b704e48a
*/
export const mergeDeep = (target, source) => {
if (isObject(target) && isObject(source)) {
keys(source).forEach(key => {
if (isObject(source[key])) {
if (!target[key] || !isObject(target[key])) {
target[key] = source[key]
}
mergeDeep(target[key], source[key])
} else {
assign(target, { [key]: source[key] })
}
})
}
return target
}
/**
* Convenience method to create a read-only descriptor
*/
export const readonlyDescriptor = () => ({ enumerable: true, configurable: false, writable: false })
/**
* Deep-freezes and object, making it immutable / read-only
* Returns the same object passed-in, but frozen
* Freezes inner object/array/values first
* Note: This method will not work for property values using `Symbol()` as a key
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
*/
export const deepFreeze = obj => {
// Retrieve the property names defined on object/array
// Note: `keys` will ignore properties that are keyed by a `Symbol()`
const props = keys(obj)
// Iterate over each prop and recursively freeze it
props.forEach(prop => {
const value = obj[prop]
// If value is a plain object or array, we deepFreeze it
obj[prop] = value && (isPlainObject(value) || isArray(value)) ? deepFreeze(value) : value
})
return freeze(obj)
}