Underscore.js _.each() to native Array.forEach() (optional with Object.entries/values)

This commit is contained in:
djmaze 2020-07-22 14:49:18 +02:00
parent 178e5f6ef7
commit a5d41edb24
25 changed files with 72 additions and 79 deletions

View file

@ -1,4 +1,3 @@
import _ from '_';
import { isObject, isUnd } from 'Common/Utils';
import * as Plugins from 'Common/Plugins';
@ -14,7 +13,7 @@ export function sub(name, func, context) {
context = func || null;
func = null;
_.each(name, (subFunc, subName) => {
Object.entries(name).forEach(([subFunc, subName]) => {
sub(subName, subFunc, context);
});
} else {
@ -34,7 +33,7 @@ export function pub(name, args) {
Plugins.runHook('rl-pub', [name, args]);
if (!isUnd(SUBS[name])) {
_.each(SUBS[name], (items) => {
SUBS[name].forEach(items => {
if (items[0]) {
items[0].apply(items[1] || null, args || []);
}

View file

@ -1,4 +1,3 @@
import _ from '_';
import { isFunc, isArray, isUnd } from 'Common/Utils';
import { data as GlobalsData } from 'Common/Globals';
import * as Settings from 'Storage/Settings';
@ -27,7 +26,7 @@ export function addHook(name, callback) {
*/
export function runHook(name, args = []) {
if (isArray(SIMPLE_HOOKS[name])) {
_.each(SIMPLE_HOOKS[name], (callback) => {
SIMPLE_HOOKS[name].forEach(callback => {
callback(...args);
});
}
@ -78,7 +77,7 @@ export function addSettingsViewModelForAdmin(SettingsViewModelClass, template, l
*/
export function runSettingsViewModelHooks(admin) {
const Knoin = require('Knoin/Knoin');
_.each(admin ? ADMIN_VIEW_MODELS_HOOKS : USER_VIEW_MODELS_HOOKS, (view) => {
(admin ? ADMIN_VIEW_MODELS_HOOKS : USER_VIEW_MODELS_HOOKS).forEach(view => {
Knoin.addSettingsViewModel(view[0], view[1], view[2], view[3]);
});
}

View file

@ -75,7 +75,7 @@ class Selector {
this.selectedItem.subscribe((item) => {
if (item) {
if (this.isListChecked()) {
_.each(this.listChecked(), (subItem) => {
this.listChecked().forEach(subItem => {
subItem.checked(false);
});
}
@ -110,7 +110,7 @@ class Selector {
this.list.subscribe(
(items) => {
if (isArray(items)) {
_.each(items, (item) => {
items.forEach(item => {
if (item) {
const uid = this.getItemUid(item);
@ -150,7 +150,7 @@ class Selector {
if (isArray(aItems)) {
len = aCheckedCache.length;
_.each(aItems, (item) => {
aItems.forEach(item => {
const uid = this.getItemUid(item);
uids.push(uid);
@ -416,7 +416,7 @@ class Selector {
EventKeyCode.Insert === iEventKeyCode ||
EventKeyCode.Space === iEventKeyCode
) {
_.each(list, (item) => {
list.forEach(item => {
if (!isStop) {
switch (iEventKeyCode) {
case EventKeyCode.Up:

View file

@ -302,7 +302,7 @@ export function replySubjectAdd(prefix, subject) {
prefixIsRe = !fwd;
if ('' !== subject) {
_.each(subject.split(':'), (part) => {
subject.split(':').forEach(part => {
const trimmedPart = trim(part);
if (!drop && (/^(RE|FWD)$/i.test(trimmedPart) || /^(RE|FWD)[[(][\d]+[\])]$/i.test(trimmedPart))) {
if (!re) {
@ -1072,7 +1072,7 @@ export function disposeOne(propOrValue, value) {
export function disposeObject(object) {
if (object) {
if (isArray(object.disposables)) {
_.each(object.disposables, disposeOne);
object.disposables.forEach(disposeOne);
}
ko.utils.objectForEach(object, disposeOne);
@ -1086,7 +1086,7 @@ export function disposeObject(object) {
export function delegateRunOnDestroy(objectOrObjects) {
if (objectOrObjects) {
if (isArray(objectOrObjects)) {
_.each(objectOrObjects, (item) => {
objectOrObjects.forEach(item => {
delegateRunOnDestroy(item);
});
} else if (objectOrObjects && objectOrObjects.onDestroy) {