<commit_before>// NG2
import { Component, ElementRef } from '@angular/core';
// APP
import { Helpers } from './../../../../utils/Helpers';
import { NovoLabelService } from './../../../../services/novo-label-service';
// Vendor
import { Observable } from 'rxjs/Rx';

/**
 * @name: PickerResults
 *
 * @description This is the actual list of matches that gets injected into the DOM. It's also the piece that can be
 * overwritten if custom list options are needed.
 */
export class BasePickerResults {
    _term: string = '';
    selected: Array<any> = [];
    matches: any = [];
    hasError: boolean = false;
    isLoading: boolean = true;
    isStatic: boolean = true;
    config: any;
    activeMatch: any;
    parent: any;
    element: ElementRef;

    constructor(element: ElementRef) {
        this.element = element;
    }

    get term() {
        return this._term;
    }

    set term(value) {
        this._term = value;
        this.hasError = false;
        this.isLoading = true;
        this.search(value)
            .subscribe(
            results => {
                this.matches = this.isStatic ? this.filterData(results) : results;
                this.isLoading = false;
            },
            () => {
                this.hasError = value && value.length !== 0;
                this.isLoading = false;
            });
    }

    search(term, mode?) {
        let options = this.config.options;
        return Observable.fromPromise(new Promise((resolve, reject) => {
            // Check if there is match data
            if (options) {
                // Resolve the data
                if (Array.isArray(options)) {
                    this.isStatic = true;
                    // Arrays are returned immediately
                    resolve(this.structureArray(options));
                } else if (term && term.length >= (this.config.minSearchLength || 1)) {
                    if ((options.hasOwnProperty('reject') && options.hasOwnProperty('resolve')) || Object.getPrototypeOf(options).hasOwnProperty('then')) {
                        this.isStatic = false;
                        // Promises (ES6 or Deferred) are resolved whenever they resolve
                        options
                            .then(this.structureArray.bind(this))
                            .then(resolve, reject);
                    } else if (typeof options === 'function') {
                        this.isStatic = false;
                        // Promises (ES6 or Deferred) are resolved whenever they resolve
                        options(term)
                            .then(this.structureArray.bind(this))
                            .then(resolve, reject);
                    } else {
                        // All other kinds of data are rejected
                        reject('The data provided is not an array or a promise');
                        throw new Error('The data provided is not an array or a promise');
                    }
                } else {
                    if (this.config.defaultOptions) {
                        this.isStatic = false;
                        if (this.config.defaultOptions instanceof Function) {
                            resolve(this.structureArray(this.config.defaultOptions()));
                        } else {
                            resolve(this.structureArray(this.config.defaultOptions));
                        }
                    } else {
                        // No search term gets rejected
                        reject('No search term');
                    }
                }
            } else {
                // No data gets rejected
                reject('error');
            }
        }));
    }

    /**
     * @name structureArray
     * @param collection - the data once getData resolves it
     * @returns { Array }
     *
     * @description This function structures an array of nodes into an array of objects with a
     * 'name' field by default.
     */
    structureArray(collection: any): any {
        let dataArray = collection.data ? collection.data : collection;
        if (dataArray && (typeof dataArray[0] === 'string' || typeof dataArray[0] === 'number')) {
            return collection.map((item) => {
                return {
                    value: item,
                    label: item
                };
            });
        }
        return dataArray.map((data) => {
            let value = this.config.field ? data[this.config.field] : (data.value || data);
            let label = this.config.format ? Helpers.interpolate(this.config.format, data) : data.label || String(value);<commit_msg>feat(picker) - allow for valueFormat in pickers (#329)<commit_after>// NG2
import { Component, ElementRef } from '@angular/core';
// APP
import { Helpers } from './../../../../utils/Helpers';
import { NovoLabelService } from './../../../../services/novo-label-service';
// Vendor
import { Observable } from 'rxjs/Rx';

/**
 * @name: PickerResults
 *
 * @description This is the actual list of matches that gets injected into the DOM. It's also the piece that can be
 * overwritten if custom list options are needed.
 */
export class BasePickerResults {
    _term: string = '';
    selected: Array<any> = [];
    matches: any = [];
    hasError: boolean = false;
    isLoading: boolean = true;
    isStatic: boolean = true;
    config: any;
    activeMatch: any;
    parent: any;
    element: ElementRef;

    constructor(element: ElementRef) {
        this.element = element;
    }

    get term() {
        return this._term;
    }

    set term(value) {
        this._term = value;
        this.hasError = false;
        this.isLoading = true;
        this.search(value)
            .subscribe(
            results => {
                this.matches = this.isStatic ? this.filterData(results) : results;
                this.isLoading = false;
            },
            () => {
                this.hasError = value && value.length !== 0;
                this.isLoading = false;
            });
    }

    search(term, mode?) {
        let options = this.config.options;
        return Observable.fromPromise(new Promise((resolve, reject) => {
            // Check if there is match data
            if (options) {
                // Resolve the data
                if (Array.isArray(options)) {
                    this.isStatic = true;
                    // Arrays are returned immediately
                    resolve(this.structureArray(options));
                } else if (term && term.length >= (this.config.minSearchLength || 1)) {
                    if ((options.hasOwnProperty('reject') && options.hasOwnProperty('resolve')) || Object.getPrototypeOf(options).hasOwnProperty('then')) {
                        this.isStatic = false;
                        // Promises (ES6 or Deferred) are resolved whenever they resolve
                        options
                            .then(this.structureArray.bind(this))
                            .then(resolve, reject);
                    } else if (typeof options === 'function') {
                        this.isStatic = false;
                        // Promises (ES6 or Deferred) are resolved whenever they resolve
                        options(term)
                            .then(this.structureArray.bind(this))
                            .then(resolve, reject);
                    } else {
                        // All other kinds of data are rejected
                        reject('The data provided is not an array or a promise');
                        throw new Error('The data provided is not an array or a promise');
                    }
                } else {
                    if (this.config.defaultOptions) {
                        this.isStatic = false;
                        if (this.config.defaultOptions instanceof Function) {
                            resolve(this.structureArray(this.config.defaultOptions()));
                        } else {
                            resolve(this.structureArray(this.config.defaultOptions));
                        }
                    } else {
                        // No search term gets rejected
                        reject('No search term');
                    }
                }
            } else {
                // No data gets rejected
                reject('error');
            }
        }));
    }

    /**
     * @name structureArray
     * @param collection - the data once getData resolves it
     * @returns { Array }
     *
     * @description This function structures an array of nodes into an array of objects with a
     * 'name' field by default.
     */
    structureArray(collection: any): any {
        let dataArray = collection.data ? collection.data : collection;
        if (dataArray && (typeof dataArray[0] === 'string' || typeof dataArray[0] === 'number')) {
            return collection.map((item) => {
                return {
                    value: item,
                    label: item
                };
            });
        }
        return dataArray.map((data) => {
            let value = this.config.field ? data[this.config.field] : (data.value || data);
            if (this.config.valueFormat) {
                value = Helpers.interpolate(this.config.valueFormat, data);
            }
            let label = this.config.format ? Helpers.interpolate(this.config.format, data) : data.label || String(value);
{'fraction_non_alphanumeric': 0.10887949260042283, 'fraction_numerical': 0.0017970401691331924, 'mean_word_length': 1.4110601427115188, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '6200065', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2210, 'n_words': 626}