93 lines
2.1 KiB
JavaScript
93 lines
2.1 KiB
JavaScript
|
/*!
|
||
|
* lunr.utils
|
||
|
* Copyright (C) @YEAR Oliver Nightingale
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* A namespace containing utils for the rest of the lunr library
|
||
|
* @namespace lunr.utils
|
||
|
*/
|
||
|
lunr.utils = {}
|
||
|
|
||
|
/**
|
||
|
* Print a warning message to the console.
|
||
|
*
|
||
|
* @param {String} message The message to be printed.
|
||
|
* @memberOf lunr.utils
|
||
|
* @function
|
||
|
*/
|
||
|
lunr.utils.warn = (function (global) {
|
||
|
/* eslint-disable no-console */
|
||
|
return function (message) {
|
||
|
if (global.console && console.warn) {
|
||
|
console.warn(message)
|
||
|
}
|
||
|
}
|
||
|
/* eslint-enable no-console */
|
||
|
})(this)
|
||
|
|
||
|
/**
|
||
|
* Convert an object to a string.
|
||
|
*
|
||
|
* In the case of `null` and `undefined` the function returns
|
||
|
* the empty string, in all other cases the result of calling
|
||
|
* `toString` on the passed object is returned.
|
||
|
*
|
||
|
* @param {Any} obj The object to convert to a string.
|
||
|
* @return {String} string representation of the passed object.
|
||
|
* @memberOf lunr.utils
|
||
|
*/
|
||
|
lunr.utils.asString = function (obj) {
|
||
|
if (obj === void 0 || obj === null) {
|
||
|
return ""
|
||
|
} else {
|
||
|
return obj.toString()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Clones an object.
|
||
|
*
|
||
|
* Will create a copy of an existing object such that any mutations
|
||
|
* on the copy cannot affect the original.
|
||
|
*
|
||
|
* Only shallow objects are supported, passing a nested object to this
|
||
|
* function will cause a TypeError.
|
||
|
*
|
||
|
* Objects with primitives, and arrays of primitives are supported.
|
||
|
*
|
||
|
* @param {Object} obj The object to clone.
|
||
|
* @return {Object} a clone of the passed object.
|
||
|
* @throws {TypeError} when a nested object is passed.
|
||
|
* @memberOf Utils
|
||
|
*/
|
||
|
lunr.utils.clone = function (obj) {
|
||
|
if (obj === null || obj === undefined) {
|
||
|
return obj
|
||
|
}
|
||
|
|
||
|
var clone = Object.create(null),
|
||
|
keys = Object.keys(obj)
|
||
|
|
||
|
for (var i = 0; i < keys.length; i++) {
|
||
|
var key = keys[i],
|
||
|
val = obj[key]
|
||
|
|
||
|
if (Array.isArray(val)) {
|
||
|
clone[key] = val.slice()
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if (typeof val === 'string' ||
|
||
|
typeof val === 'number' ||
|
||
|
typeof val === 'boolean') {
|
||
|
clone[key] = val
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
throw new TypeError("clone is not deep and does not support nested objects")
|
||
|
}
|
||
|
|
||
|
return clone
|
||
|
}
|