mixin
util
Adds all own enumerable string keyed function properties of a source object to the destination object.
Installation
Import
import { mixin } from '@tulx/utils';Source Code
Implementation
/**
* Adds all own enumerable string keyed function properties of a source object to the destination object.
*
* @param object - The destination object.
* @param source - The object of functions to add.
* @param options - The options object.
* @returns Returns object.
*
* @example
* ```ts
* function vowels(string) {
* return string.match(/[aeiou]/g);
* }
* mixin({}, { 'vowels': vowels });
* ```
*/
export function mixin<T extends Record<string, unknown>>(
object: T,
source: Record<string, unknown>,
_options?: { chain?: boolean }
): T {
const result = object as Record<string, unknown>;
for (const key in source) {
if (
Object.prototype.hasOwnProperty.call(source, key) &&
typeof source[key] === 'function'
) {
result[key] = source[key];
}
}
return object;
}
Example
import { mixin } from '@tulx/utils';
function vowels(string) {
return string.match(/[aeiou]/g);
}
mixin({}, { 'vowels': vowels });Related Functions
attempt
Attempts to invoke func, returning either the result or the caught error object.
bindAll
Binds methods of an object to the object itself, overwriting the existing method.
cond
Creates a function that iterates over pairs and invokes the corresponding function of the first predicate to return truthy.
conforms
Creates a function that invokes the predicate properties of source with the corresponding property values of a given object.
constant
Creates a function that returns value.
defaultTo
Checks value to determine whether a default value should be returned in its place.