Skip to content

Commit 0d8a568

Browse files
committed
Optimize some and every for synchronous execution
1 parent 8f5f7b8 commit 0d8a568

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

defaultMethods.js

+31-21
Original file line numberDiff line numberDiff line change
@@ -433,15 +433,34 @@ const defaultMethods = {
433433
}
434434
},
435435
map: createArrayIterativeMethod('map'),
436-
some: createArrayIterativeMethod('some', true),
436+
some: {
437+
...createArrayIterativeMethod('some', true),
438+
method: (input, context, above, engine) => {
439+
if (!Array.isArray(input)) throw new InvalidControlInput(input)
440+
let [selector, mapper] = input
441+
442+
selector = engine.run(selector, context, { above }) || []
443+
444+
for (let i = 0; i < selector.length; i++) {
445+
if (engine.truthy(engine.run(mapper, selector[i], { above: [selector, context, above] }))) return true
446+
}
447+
return false
448+
}
449+
},
437450
all: {
438451
[Sync]: oldAll[Sync],
439452
method: (args, context, above, engine) => {
440453
if (Array.isArray(args)) {
441454
const first = engine.run(args[0], context, above)
442455
if (Array.isArray(first) && first.length === 0) return false
443456
}
444-
return oldAll.method(args, context, above, engine)
457+
let [selector, mapper] = args
458+
selector = engine.run(selector, context, { above }) || []
459+
460+
for (let i = 0; i < selector.length; i++) {
461+
if (!engine.truthy(engine.run(mapper, selector[i], { above: [selector, context, above] }))) return false
462+
}
463+
return true
445464
},
446465
asyncMethod: async (args, context, above, engine) => {
447466
if (Array.isArray(args)) {
@@ -456,13 +475,8 @@ const defaultMethods = {
456475
none: {
457476
[Sync]: (data, buildState) => isSyncDeep(data, buildState.engine, buildState),
458477
lazy: true,
459-
// todo: add async build & build
460-
method: (val, context, above, engine) => {
461-
return !defaultMethods.some.method(val, context, above, engine)
462-
},
463-
asyncMethod: async (val, context, above, engine) => {
464-
return !(await defaultMethods.some.asyncMethod(val, context, above, engine))
465-
},
478+
method: (val, context, above, engine) => !defaultMethods.some.method(val, context, above, engine),
479+
asyncMethod: async (val, context, above, engine) => !(await defaultMethods.some.asyncMethod(val, context, above, engine)),
466480
compile: (data, buildState) => {
467481
const result = defaultMethods.some.compile(data, buildState)
468482
return result ? buildState.compile`!(${result})` : false
@@ -480,7 +494,6 @@ const defaultMethods = {
480494
}
481495
return result
482496
},
483-
every: createArrayIterativeMethod('every'),
484497
filter: createArrayIterativeMethod('filter', true),
485498
reduce: {
486499
deterministic: (data, buildState) => {
@@ -768,26 +781,21 @@ function createArrayIterativeMethod (name, useTruthy = false) {
768781
method: (input, context, above, engine) => {
769782
if (!Array.isArray(input)) throw new InvalidControlInput(input)
770783
let [selector, mapper] = input
771-
selector =
772-
engine.run(selector, context, {
773-
above
774-
}) || []
784+
785+
selector = engine.run(selector, context, { above }) || []
775786

776787
return selector[name]((i, index) => {
777-
const result = engine.run(mapper, i, {
778-
above: [{ iterator: selector, index }, context, above]
779-
})
788+
if (!mapper || typeof mapper !== 'object') return useTruthy ? engine.truthy(mapper) : mapper
789+
const result = engine.run(mapper, i, { above: [{ iterator: selector, index }, context, above] })
780790
return useTruthy ? engine.truthy(result) : result
781791
})
782792
},
783793
asyncMethod: async (input, context, above, engine) => {
784794
if (!Array.isArray(input)) throw new InvalidControlInput(input)
785795
let [selector, mapper] = input
786-
selector =
787-
(await engine.run(selector, context, {
788-
above
789-
})) || []
796+
selector = (await engine.run(selector, context, { above })) || []
790797
return asyncIterators[name](selector, async (i, index) => {
798+
if (!mapper || typeof mapper !== 'object') return useTruthy ? engine.truthy(mapper) : mapper
791799
const result = await engine.run(mapper, i, {
792800
above: [{ iterator: selector, index }, context, above]
793801
})
@@ -822,6 +830,8 @@ function createArrayIterativeMethod (name, useTruthy = false) {
822830
lazy: true
823831
}
824832
}
833+
834+
defaultMethods.every = defaultMethods.all
825835
defaultMethods['?:'] = defaultMethods.if
826836
// declare all of the functions here synchronous
827837
Object.keys(defaultMethods).forEach((item) => {

0 commit comments

Comments
 (0)