<commit_before>/*
 * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

export default class GoldenLayoutContentUtils {

    /**
     * Extract and returns content for the specified widget from the given GoldenLayout contents.
     * @param {string} widgetUuid widget UUID
     * @param {array} goldenLayoutContents GoldenLayout content
     * @returns {{title: string, component: string, props: {id: string, config: object}} | null} widget content config
     */
    static getWidgetContent(widgetUuid, goldenLayoutContents) {
        return GoldenLayoutContentUtils._traverseWidgetContents(goldenLayoutContents, function (widgetContent) {
            return widgetContent.props && (widgetContent.props.id === widgetUuid);
        });
    }

    /**
     * Returns names of all widgets referred in the given GoldenLayout contents.
     * @param {array} goldenLayoutContents GoldenLayout content
     * @returns {Set} mentioned widget names
     */
    static getReferredWidgetNames(goldenLayoutContents) {
        let widgetNames = new Set();
        GoldenLayoutContentUtils._traverseWidgetContents(goldenLayoutContents,
                                                         widgetContent => widgetNames.add(widgetContent.component));
        return widgetNames;
    }

    static _traverseWidgetContents(goldenLayoutContents, consumer) {
        if (!Array.isArray(goldenLayoutContents)) {
            return null;
        }

        for (let i = 0; i < goldenLayoutContents.length; i++) {
            let content = goldenLayoutContents[i];
            if (!content) {
                continue;
            }
            if ((content.type === 'component') && (content.componentName === 'lm-react-component') &&
                consumer.consume(content)) {
                return content;
            } else {
                content = GoldenLayoutContentUtils._traverseWidgetContents(content.content, consumer);
                if (content) {
                    return content;
                }
            }
        }
        return null;
    }
}
<commit_msg>Fix issue in traverse logic in GoldenLayoutContentUtils.<commit_after>/*
 * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

export default class GoldenLayoutContentUtils {

    /**
     * Extract and returns content for the specified widget from the given GoldenLayout contents.
     * @param {string} widgetUuid widget UUID
     * @param {array} goldenLayoutContents GoldenLayout content
     * @returns {{title: string, component: string, props: {id: string, config: object}} | null} widget content config
     */
    static getWidgetContent(widgetUuid, goldenLayoutContents) {
        return GoldenLayoutContentUtils._traverseWidgetContents(goldenLayoutContents, function (widgetContent) {
            return widgetContent.props && (widgetContent.props.id === widgetUuid);
        });
    }

    /**
     * Returns names of all widgets referred in the given GoldenLayout contents.
     * @param {array} goldenLayoutContents GoldenLayout content
     * @returns {Set} mentioned widget names
     */
    static getReferredWidgetNames(goldenLayoutContents) {
        let widgetNames = new Set();
        GoldenLayoutContentUtils._traverseWidgetContents(goldenLayoutContents, widgetContent => {
            widgetNames.add(widgetContent.component);
            return false;
        });
        return Array.from(widgetNames.values());
    }

    static _traverseWidgetContents(goldenLayoutContents, consumer) {
        if (!Array.isArray(goldenLayoutContents)) {
            return null;
        }

        for (let i = 0; i < goldenLayoutContents.length; i++) {
            let content = goldenLayoutContents[i];
            if (!content) {
                continue;
            }
            if ((content.type === 'component') && (content.componentName === 'lm-react-component')) {
                if (consumer(content)) {
                    return content;
                }
            } else {
                content = GoldenLayoutContentUtils._traverseWidgetContents(content.content, consumer);
                if (content) {
                    return content;
                }
            }
        }
        return null;
    }
}

{'fraction_non_alphanumeric': 0.08777084098420859, 'fraction_numerical': 0.0044069041498347415, 'mean_word_length': 2.5096649484536084, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3874480', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1305, 'n_words': 441}