PageSourceSearch

https://www.springerpub.com/static/version1789146322/base/Magento/base/default/mage/utils/arrays.js

js springerpub.com collected 2026-09-24 08:47:42 UTC 4,714 bytes, 161 lines download raw bytes

1/**
2 * Copyright © Magento, Inc. All rights reserved.
3 * See COPYING.txt for license details.
4 */
5
6define([
7    'underscore',
8    './strings'
9], function (_, utils) {
10    'use strict';
11
12    /**
13     * Defines index of an item in a specified container.
14     *
15     * @param {*} item - Item whose index should be defined.
16     * @param {Array} container - Container upon which to perform search.
17     * @returns {Number}
18     */
19    function getIndex(item, container) {
20        var index = container.indexOf(item);
21
22        if (~index) {
23            return index;
24        }
25
26        return _.findIndex(container, function (value) {
27            return value && value.name === item;
28        });
29    }
30
31    return {
32        /**
33         * Facade method to remove/add value from/to array
34         * without creating a new instance.
35         *
36         * @param {Array} arr - Array to be modified.
37         * @param {*} value - Value to add/remove.
38         * @param {Boolean} add - Flag that specfies operation.
39         * @returns {Utils} Chainable.
40         */
41        toggle: function (arr, value, add) {
42            return add ?
43                this.add(arr, value) :
44                this.remove(arr, value);
45        },
46
47        /**
48         * Removes the incoming value from array in case
49         * without creating a new instance of it.
50         *
51         * @param {Array} arr - Array to be modified.
52         * @param {*} value - Value to be removed.
53         * @returns {Utils} Chainable.
54         */
55        remove: function (arr, value) {
56            var index = arr.indexOf(value);
57
58            if (~index) {
59                arr.splice(index, 1);
60            }
61
62            return this;
63        },
64
65        /**
66         * Adds the incoming value to array if
67         * it's not alredy present in there.
68         *
69         * @param {Array} arr - Array to be modifed.
70         * @param {...*} arguments - Values to be added.
71         * @returns {Utils} Chainable.
72         */
73        add: function (arr) {
74            var values = _.toArray(arguments).slice(1);
75
76            values.forEach(function (value) {
77                if (!~arr.indexOf(value)) {
78                    arr.push(value);
79                }
80            });
81
82            return this;
83        },
84
85        /**
86         * Inserts specified item into container at a specified position.
87         *
88         * @param {*} item - Item to be inserted into container.
89         * @param {Array} container - Container of items.
90         * @param {*} [position=-1] - Position at which item should be inserted.
91         *      Position can represent:
92         *          - specific index in container
93         *          - item which might already be present in container
94         *          - structure with one of these properties: after, before
95         * @returns {Boolean|*}
96         *      - true if element has changed its' position
97         *      - false if nothing has changed
98         *      - inserted value if it wasn't present in container
99         */
100        insert: function (item, container, position) {
101            var currentIndex = getIndex(item, container),
102                newIndex,
103                target;
104
105            if (typeof position === 'undefined') {
106                position = -1;
107            } else if (typeof position === 'string') {
108                position = isNaN(+position) ? position : +position;
109            }
110
111            newIndex = position;
112
113            if (~currentIndex) {
114                target = container.splice(currentIndex, 1)[0];
115
116                if (typeof item === 'string') {
117                    item = target;
118                }
119            }
120
121            if (typeof position !== 'number') {
122                target = position.after || position.before || position;
123
124                newIndex = getIndex(target, container);
125
126                if (~newIndex && (position.after || newIndex >= currentIndex)) {
127                    newIndex++;
128                }
129            }
130
131            if (newIndex < 0) {
132                newIndex += container.length + 1;
133            }
134
135            container[newIndex] ?
136                container.splice(newIndex, 0, item) :
137                container[newIndex] = item;
138
139            return !~currentIndex ? item : currentIndex !== newIndex;
140        },
141
142        /**
143         * @param {Array} elems
144         * @param {Number} offset
145         * @return {Number|*}
146         */
147        formatOffset: function (elems, offset) {
148            if (utils.isEmpty(offset)) {
149                offset = -1;
150            }
151
152            offset = +offset;
153
154            if (offset < 0) {
155                offset += elems.length + 1;
156            }
157
158            return offset;
159        }
160    };
161});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.