How to use the @lwc/shared.isFunction function in @lwc/shared

To help you get started, we’ve selected a few @lwc/shared examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github salesforce / lwc / packages / @lwc / engine / src / framework / decorators / decorate.ts View on Github external
export default function decorate(Ctor: any, decorators: DecoratorMap): any {
    // intentionally comparing decorators with null and undefined
    if (!isFunction(Ctor) || decorators == null) {
        throw new TypeError();
    }
    const props = getOwnPropertyNames(decorators);
    // intentionally allowing decoration of classes only for now
    const target = Ctor.prototype;
    for (let i = 0, len = props.length; i < len; i += 1) {
        const propName = props[i];
        const decorator = decorators[propName];
        if (!isFunction(decorator)) {
            throw new TypeError();
        }
        const originalDescriptor = getOwnPropertyDescriptor(target, propName);
        const descriptor = decorator(Ctor, propName, originalDescriptor);
        if (!isUndefined(descriptor)) {
            defineProperty(target, propName, descriptor);
        }
    }
    return Ctor; // chaining
}
github salesforce / lwc / packages / @lwc / engine / src / framework / decorators / api.ts View on Github external
);
        if (isObject(descriptor) && isFunction(descriptor.set)) {
            assert.isTrue(
                isObject(descriptor) && isFunction(descriptor.get),
                `Missing getter for property ${toString(
                    propName
                )} decorated with @api in ${target}. You cannot have a setter without the corresponding getter.`
            );
        }
    }
    const meta = getDecoratorsRegisteredMeta(target);
    // initializing getters and setters for each public prop on the target prototype
    if (isObject(descriptor) && (isFunction(descriptor.get) || isFunction(descriptor.set))) {
        // if it is configured as an accessor it must have a descriptor
        // @ts-ignore it must always be set before calling this method
        meta.props[propName].config = isFunction(descriptor.set) ? 3 : 1;
        return createPublicAccessorDescriptor(target, propName, descriptor);
    } else {
        // @ts-ignore it must always be set before calling this method
        meta.props[propName].config = 0;
        return createPublicPropertyDescriptor(target, propName, descriptor);
    }
}
github salesforce / lwc / packages / @lwc / engine / src / framework / template.ts View on Github external
export function evaluateTemplate(vm: VM, html: Template): Array {
    if (process.env.NODE_ENV !== 'production') {
        assert.isTrue(
            isFunction(html),
            `evaluateTemplate() second argument must be an imported template instead of ${toString(
                html
            )}`
        );
    }
    const isUpdatingTemplateInception = isUpdatingTemplate;
    const vmOfTemplateBeingUpdatedInception = vmBeingRendered;
    let vnodes: VNodes = [];

    runWithBoundaryProtection(
        vm,
        vm.owner,
        () => {
            // pre
            vmBeingRendered = vm;
            if (process.env.NODE_ENV !== 'production') {
github salesforce / lwc / packages / @lwc / engine / src / framework / api.ts View on Github external
return function(event: Event) {
        // located service for the locator metadata
        const {
            context: { locator },
        } = vm;
        if (!isUndefined(locator)) {
            const { locator: locatorService } = Services;
            if (locatorService) {
                locator.resolved = {
                    target: id,
                    host: locator.id,
                    targetContext: isFunction(context) && context(),
                    hostContext: isFunction(locator.context) && locator.context(),
                };
                // a registered `locator` service will be invoked with
                // access to the context.locator.resolved, which will contain:
                // outer id, outer context, inner id, and inner context
                invokeServiceHook(vm, locatorService);
            }
        }
        // invoke original event listener via b()
        eventListener(event);
    };
}
github salesforce / lwc / packages / @lwc / engine / src / framework / api.ts View on Github external
export function c(
    sel: string,
    Ctor: ComponentConstructor,
    data: CustomElementCompilerData,
    children?: VNodes
): VCustomElement {
    if (isCircularModuleDependency(Ctor)) {
        Ctor = resolveCircularModuleDependency(Ctor);
    }
    const vmBeingRendered = getVMBeingRendered();
    if (process.env.NODE_ENV !== 'production') {
        assert.isTrue(isString(sel), `c() 1st argument sel must be a string.`);
        assert.isTrue(isFunction(Ctor), `c() 2nd argument Ctor must be a function.`);
        assert.isTrue(isObject(data), `c() 3nd argument data must be an object.`);
        assert.isTrue(
            arguments.length === 3 || isArray(children),
            `c() 4nd argument data must be an array.`
        );
        // checking reserved internal data properties
        assert.isFalse(
            data.className && data.classMap,
            `vnode.data.className and vnode.data.classMap ambiguous declaration.`
        );
        assert.isFalse(
            data.styleMap && data.style,
            `vnode.data.styleMap and vnode.data.style ambiguous declaration.`
        );
        if (data.style && !isString(data.style)) {
            logError(
github salesforce / lwc / packages / @lwc / engine / src / framework / decorators / api.ts View on Github external
export default function api(
    target: ComponentConstructor,
    propName: PropertyKey,
    descriptor: PropertyDescriptor | undefined
): PropertyDescriptor {
    if (process.env.NODE_ENV !== 'production') {
        if (arguments.length !== 3) {
            assert.fail(`@api decorator can only be used as a decorator function.`);
        }
    }
    if (process.env.NODE_ENV !== 'production') {
        assert.invariant(
            !descriptor || isFunction(descriptor.get) || isFunction(descriptor.set),
            `Invalid property ${toString(
                propName
            )} definition in ${target}, it cannot be a prototype definition if it is a public property. Instead use the constructor to define it.`
        );
        if (isObject(descriptor) && isFunction(descriptor.set)) {
            assert.isTrue(
                isObject(descriptor) && isFunction(descriptor.get),
                `Missing getter for property ${toString(
                    propName
                )} decorated with @api in ${target}. You cannot have a setter without the corresponding getter.`
            );
        }
    }
    const meta = getDecoratorsRegisteredMeta(target);
    // initializing getters and setters for each public prop on the target prototype
    if (isObject(descriptor) && (isFunction(descriptor.get) || isFunction(descriptor.set))) {
github salesforce / lwc / packages / @lwc / engine / src / framework / def.ts View on Github external
export function isComponentConstructor(ctor: any): ctor is ComponentConstructor {
    if (!isFunction(ctor)) {
        return false;
    }

    // Fast path: LightningElement is part of the prototype chain of the constructor.
    if (ctor.prototype instanceof BaseLightningElement) {
        return true;
    }

    // Slow path: LightningElement is not part of the prototype chain of the constructor, we need
    // climb up the constructor prototype chain to check in case there are circular dependencies
    // to resolve.
    let current = ctor;
    do {
        if (isCircularModuleDependency(current)) {
            const circularResolved = resolveCircularModuleDependency(current);
github salesforce / lwc / packages / @lwc / engine / src / framework / decorators / register.ts View on Github external
return publicMethods.reduce((methodsHash: MethodDef, methodName: string): MethodDef => {
        if (process.env.NODE_ENV !== 'production') {
            assert.isTrue(
                isFunction(target.prototype[methodName]),
                `Component "${target.name}" should have a method \`${methodName}\` instead of ${target.prototype[methodName]}.`
            );
        }
        methodsHash[methodName] = target.prototype[methodName];
        return methodsHash;
    }, create(null));
}
github salesforce / lwc / packages / @lwc / engine / src / framework / base-lightning-element.ts View on Github external
function createBridgeToElementDescriptor(
    propName: string,
    descriptor: PropertyDescriptor
): PropertyDescriptor {
    const { get, set, enumerable, configurable } = descriptor;
    if (!isFunction(get)) {
        if (process.env.NODE_ENV !== 'production') {
            assert.fail(
                `Detected invalid public property descriptor for HTMLElement.prototype.${propName} definition. Missing the standard getter.`
            );
        }
        throw new TypeError();
    }
    if (!isFunction(set)) {
        if (process.env.NODE_ENV !== 'production') {
            assert.fail(
                `Detected invalid public property descriptor for HTMLElement.prototype.${propName} definition. Missing the standard setter.`
            );
        }
        throw new TypeError();
    }
    return {