<commit_before>import {
    hasOwn,
    every,
    each,
    eachDict,
    toArray,
    isArray,
    isObject,
    isFunction,
    validArray,
    observableArray
} from './';

export const linkedLabel = '__hasLinked';
export const unlinkMethodLabel = '__unlink';

function isArrayObservable(target) {
    return ko.isObservable(target) && isFunction(target.push);
}

// Link array observable with validators
//
// @param {Function} observable
// @param {Object|Function} validator
export function linkArrayObservable(observable, validator) {
    if (!isArrayObservable(observable) || observable[linkedLabel]) {
        return;
    }

    const computedObservable = ko.unwrap(observable);
    const originPush = observable.push;
    const originUnshift = observable.unshift;
    const originSplice = observable.splice;

    each(computedObservable, (item) => {
        linkArrayObservable(item);
    });

    observable[unlinkMethodLabel] = () => {
        observable.push = originPush;
        observable.unshift = originUnshift;
        observable.splice = originSplice;
        delete observable[linkedLabel];
        delete observable[unlinkMethodLabel];
    };

    observable.push = (item) => {
        const validResult = {};

        if (validArray('push', [ item ], validResult, validator)) {
            observableArray(validResult['push']);
            return originPush.call(observable, validResult['push'][0]);
        }
    };

    observable.unshift = (item) => {
        const validResult = {};

        if (validArray('unshift', [ item ], validResult, validator)) {
            observableArray(validResult['unshift']);
            return originUnshift.call(observable, validResult['unshift'][0]);
        }
    };

    observable.splice = function () {
        if (arguments.length < 3) {
            return;
        }

        const args = [ arguments[0], arguments[1] ];
        const result = every(toArray(arguments, 2), (item, i) => {
            const subValidResult = {};
            const subResult = validArray('splice', [ item ], subValidResult, validator);

            if (subResult) {
                observableArray(subValidResult['splice']);
                args.push(subValidResult['splice'][0]);
            }

            return subResult;
        });

        if (result) {
            return originSplice.apply(observable, args);
        }
    };

    observable[linkedLabel] = true;
};<commit_msg>Link support update<commit_after>import {
    hasOwn,
    every,
    each,
    eachDict,
    toArray,
    isArray,
    isObject,
    isFunction,
    validArray,
    observableArray
} from './';

export const linkedLabel = '__hasLinked';

function isArrayObservable(target) {
    return ko.isObservable(target) && isFunction(target.push);
}

// Link array observable with validators
//
// @param {Function} observable
// @param {Object|Function} validator
export function linkArrayObservable(observable, validator) {
    if (!isArrayObservable(observable) || observable[linkedLabel]) {
        return;
    }

    each(ko.unwrap(observable), (item) => {
        linkArrayObservable(item);
    });
    observable.subscribe((changes) => {
        const validResult = {};
        const items = [];

        each(changes, (change) => {
            if (change.status === 'added') {
                items.push(change.value);
            }
        });

        if (validArray('link', items, validResult, validator)) {
            observableArray(observable());
        }
    }, null, 'arrayChange');
    observable[linkedLabel] = true;
};
{'fraction_non_alphanumeric': 0.1271043771043771, 'fraction_numerical': 0.001964085297418631, 'mean_word_length': 2.5685685685685686, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 11, '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': '460200', 'n_tokens_mistral': 1025, 'n_tokens_neox': 996, 'n_words': 199}