Passed
Push — trunk ( 88e2f6...97604d )
by Christian
15:50 queued 12s
created

util.service.ts ➔ moveItem   A

Complexity

Conditions 4

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
dl 0
loc 29
rs 9.328
c 0
b 0
f 0
1
/**
2
 * @package admin
3
 *
4
 * @module core/service/utils
5
 */
6
import throttle from 'lodash/throttle';
7
import flow from 'lodash/flow';
8
import debounce from 'lodash/debounce';
9
import flattenDeep from 'lodash/flattenDeep';
10
import { v4 as uuidv4 } from 'uuid';
11
import remove from 'lodash/remove';
12
import slice from 'lodash/slice';
13
import uniqBy from 'lodash/uniqBy';
14
import chunk from 'lodash/chunk';
15
import intersectionBy from 'lodash/intersectionBy';
16
17
import {
18
    deepCopyObject,
19
    hasOwnProperty,
20
    getObjectDiff,
21
    getArrayChanges,
22
    cloneDeep,
23
    merge,
24
    mergeWith,
25
    deepMergeObject,
26
    get,
27
    set,
28
    pick,
29
} from './utils/object.utils';
30
import { warn, error } from './utils/debug.utils';
31
import { currency, date, dateWithUserTimezone, fileSize, md5, toISODate } from './utils/format.utils';
32
import domUtils from './utils/dom.utils';
33
import stringUtils from './utils/string.utils';
34
import typesUtils, { isUndefined } from './utils/types.utils';
35
import fileReaderUtils from './utils/file-reader.utils';
36
import sortUtils from './utils/sort.utils';
37
38
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
39
export const object = {
40
    deepCopyObject: deepCopyObject,
41
    hasOwnProperty: hasOwnProperty,
42
    getObjectDiff: getObjectDiff,
43
    getArrayChanges: getArrayChanges,
44
    cloneDeep: cloneDeep,
45
    merge: merge,
46
    mergeWith: mergeWith,
47
    deepMergeObject: deepMergeObject,
48
    get: get,
49
    set: set,
50
    pick: pick,
51
};
52
53
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
54
export const debug = {
55
    warn: warn,
56
    error: error,
57
};
58
59
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
60
export const format = {
61
    currency: currency,
62
    date: date,
63
    dateWithUserTimezone: dateWithUserTimezone,
64
    fileSize: fileSize,
65
    md5: md5,
66
    toISODate: toISODate,
67
};
68
69
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
70
export const dom = {
71
    getScrollbarHeight: domUtils.getScrollbarHeight,
72
    getScrollbarWidth: domUtils.getScrollbarWidth,
73
    copyToClipboard: domUtils.copyToClipboard,
74
    copyStringToClipboard: domUtils.copyStringToClipboard,
75
};
76
77
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
78
export const string = {
79
    capitalizeString: stringUtils.capitalizeString,
80
    camelCase: stringUtils.camelCase,
81
    upperFirst: stringUtils.upperFirst,
82
    kebabCase: stringUtils.kebabCase,
83
    snakeCase: stringUtils.snakeCase,
84
    md5: md5,
85
    isEmptyOrSpaces: stringUtils.isEmptyOrSpaces,
86
    isUrl: stringUtils.isUrl,
87
    isValidIp: stringUtils.isValidIp,
88
};
89
90
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
91
export const types = {
92
    isObject: typesUtils.isObject,
93
    isPlainObject: typesUtils.isPlainObject,
94
    isEmpty: typesUtils.isEmpty,
95
    isRegExp: typesUtils.isRegExp,
96
    isArray: typesUtils.isArray,
97
    isFunction: typesUtils.isFunction,
98
    isDate: typesUtils.isDate,
99
    isString: typesUtils.isString,
100
    isBoolean: typesUtils.isBoolean,
101
    isEqual: typesUtils.isEqual,
102
    isNumber: typesUtils.isNumber,
103
    isUndefined: isUndefined,
104
};
105
106
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
107
export const fileReader = {
108
    readAsArrayBuffer: fileReaderUtils.readFileAsArrayBuffer,
109
    readAsDataURL: fileReaderUtils.readFileAsDataURL,
110
    readAsText: fileReaderUtils.readFileAsText,
111
    getNameAndExtensionFromFile: fileReaderUtils.getNameAndExtensionFromFile,
112
    getNameAndExtensionFromUrl: fileReaderUtils.getNameAndExtensionFromUrl,
113
};
114
115
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
116
export const sort = {
117
    afterSort: sortUtils.afterSort,
118
};
119
120
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
121
export const array = {
122
    flattenDeep: flattenDeep,
123
    remove: remove,
124
    slice: slice,
125
    uniqBy: uniqBy,
126
    chunk: chunk,
127
    intersectionBy: intersectionBy,
128
};
129
130
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
131
export default {
132
    createId,
133
    throttle,
134
    debounce,
135
    flow,
136
    get,
137
    object,
138
    debug,
139
    format,
140
    dom,
141
    string,
142
    types,
143
    fileReader,
144
    sort,
145
    array,
146
    moveItem,
147
};
148
149
/**
150
 * Returns an uuid string in hex format.
151
 *
152
 * @returns { String }
153
 */
154
function createId(): string {
155
    // eslint-disable-next-line max-len
156
    // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-member-access
157
    return uuidv4().replace(/-/g, '');
158
}
159
160
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
161
export function moveItem(
162
    entity: MutationObserver[],
163
    oldIndex: number,
164
    newIndex: number,
165
) {
166
    if (newIndex === null) {
167
        newIndex = entity.length;
168
    }
169
170
    if (oldIndex < 0 || oldIndex >= entity.length || newIndex === oldIndex) {
171
        return;
172
    }
173
174
    const movedItem = entity.find((_, index) => index === oldIndex);
175
    if (!movedItem) {
176
        return;
177
    }
178
179
    const remainingItems = entity.filter((_, index) => index !== oldIndex);
180
181
    const orderedItems = [
182
        ...remainingItems.slice(0, newIndex),
183
        movedItem,
184
        ...remainingItems.slice(newIndex),
185
    ];
186
187
    entity.splice(0, entity.length, ...orderedItems);
188
}
189