<commit_before>import {Autowired, Bean} from "../../context/context";
import {AgGridComponentFunctionInput, AgGridRegisteredComponentInput} from "./componentProvider";
import {IComponent} from "../../interfaces/iComponent";
import {ComponentMetadata, ComponentMetadataProvider} from "./componentMetadataProvider";
import {ComponentSource, ComponentType, ResolvedComponent} from "./componentResolver";
import {ICellRendererComp, ICellRendererParams} from "../../rendering/cellRenderers/iCellRenderer";
import {_} from "../../utils";

@Bean("agComponentUtils")
export class AgComponentUtils {
    @Autowired("componentMetadataProvider")
    private componentMetadataProvider:ComponentMetadataProvider;

    public adaptFunction <A extends IComponent<any> & B, B>(
        propertyName:string,
        hardcodedJsFunction: AgGridComponentFunctionInput,
        type:ComponentType,
        source:ComponentSource
    ):ResolvedComponent<A,B>{
        if (hardcodedJsFunction == null) return {
            component: null,
            type: type,
            source: source,
            dynamicParams: null
        };

        let metadata:ComponentMetadata = this.componentMetadataProvider.retrieve(propertyName);
        if (metadata && metadata.functionAdapter){
            return {
                type: type,
                component: <{new(): A}>metadata.functionAdapter(hardcodedJsFunction),
                source: source,
                dynamicParams: null
            }
        }
        return null;
    }

    public adaptCellRendererFunction (callback:AgGridComponentFunctionInput):{new(): IComponent<any>}{
        class Adapter implements ICellRendererComp{
            private params: ICellRendererParams;

            refresh(params: any): boolean {
                return false;
            }

            getGui(): HTMLElement {
                let callbackResult: string | HTMLElement = callback(this.params);
                if (typeof callbackResult != 'string') return callbackResult;

                return _.loadTemplate('<span>' + callbackResult + '</span>');
            }

            init?(params: ICellRendererParams): void {
                this.params = params;
            }

        }
        return Adapter;
    }<commit_msg>AG-1473 Cell renderer function throws an error if it returns a number, but works if it returns a string<commit_after>import {Autowired, Bean} from "../../context/context";
import {AgGridComponentFunctionInput, AgGridRegisteredComponentInput} from "./componentProvider";
import {IComponent} from "../../interfaces/iComponent";
import {ComponentMetadata, ComponentMetadataProvider} from "./componentMetadataProvider";
import {ComponentSource, ComponentType, ResolvedComponent} from "./componentResolver";
import {ICellRendererComp, ICellRendererParams} from "../../rendering/cellRenderers/iCellRenderer";
import {_} from "../../utils";

@Bean("agComponentUtils")
export class AgComponentUtils {
    @Autowired("componentMetadataProvider")
    private componentMetadataProvider:ComponentMetadataProvider;

    public adaptFunction <A extends IComponent<any> & B, B>(
        propertyName:string,
        hardcodedJsFunction: AgGridComponentFunctionInput,
        type:ComponentType,
        source:ComponentSource
    ):ResolvedComponent<A,B>{
        if (hardcodedJsFunction == null) return {
            component: null,
            type: type,
            source: source,
            dynamicParams: null
        };

        let metadata:ComponentMetadata = this.componentMetadataProvider.retrieve(propertyName);
        if (metadata && metadata.functionAdapter){
            return {
                type: type,
                component: <{new(): A}>metadata.functionAdapter(hardcodedJsFunction),
                source: source,
                dynamicParams: null
            }
        }
        return null;
    }

    public adaptCellRendererFunction (callback:AgGridComponentFunctionInput):{new(): IComponent<any>}{
        class Adapter implements ICellRendererComp{
            private params: ICellRendererParams;

            refresh(params: any): boolean {
                return false;
            }

            getGui(): HTMLElement {
                let callbackResult: string | HTMLElement = callback(this.params);
                let type = typeof callbackResult;
                if (type === 'string' || type === 'number' || type === 'boolean') {
                    return _.loadTemplate('<span>' + callbackResult + '</span>');
                } else {
                    return <HTMLElement> callbackResult;
                }
            }

            init?(params: ICellRendererParams): void {
                this.params = params;
            }

        }
        return Adapter;
    }
{'fraction_non_alphanumeric': 0.1013215859030837, 'fraction_numerical': 0.0008391021606880638, 'mean_word_length': 2.477753464624362, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6263897', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1106, 'n_words': 273}