@@ -7,7 +7,7 @@ import declareSync from './utilities/declareSync.js'
77import { build , buildString } from './compiler.js'
88import chainingSupported from './utilities/chainingSupported.js'
99import InvalidControlInput from './errors/InvalidControlInput.js'
10- import { splitPathMemoized } from './utilities/splitPath .js'
10+ import legacyMethods from './legacy .js'
1111
1212function isDeterministic ( method , engine , buildState ) {
1313 if ( Array . isArray ( method ) ) {
@@ -278,26 +278,6 @@ const defaultMethods = {
278278 if ( i && typeof i === 'object' ) return Object . keys ( i ) . length
279279 return 0
280280 } ,
281- get : {
282- method : ( [ data , key , defaultValue ] , context , above , engine ) => {
283- const notFound = defaultValue === undefined ? null : defaultValue
284-
285- const subProps = splitPathMemoized ( String ( key ) )
286- for ( let i = 0 ; i < subProps . length ; i ++ ) {
287- if ( data === null || data === undefined ) {
288- return notFound
289- }
290- // Descending into context
291- data = data [ subProps [ i ] ]
292- if ( data === undefined ) {
293- return notFound
294- }
295- }
296- if ( engine . allowFunctions || typeof data [ key ] !== 'function' ) {
297- return data
298- }
299- }
300- } ,
301281 exists : {
302282 method : ( key , context , above , engine ) => {
303283 const result = defaultMethods . val . method ( key , context , above , engine , Unfound )
@@ -384,64 +364,6 @@ const defaultMethods = {
384364 return false
385365 }
386366 } ,
387- var : ( key , context , above , engine ) => {
388- let b
389- if ( Array . isArray ( key ) ) {
390- b = key [ 1 ]
391- key = key [ 0 ]
392- }
393- let iter = 0
394- while (
395- typeof key === 'string' &&
396- key . startsWith ( '../' ) &&
397- iter < above . length
398- ) {
399- context = above [ iter ++ ]
400- key = key . substring ( 3 )
401- // A performance optimization that allows you to pass the previous above array without spreading it as the last argument
402- if ( iter === above . length && Array . isArray ( context ) ) {
403- iter = 0
404- above = context
405- context = above [ iter ++ ]
406- }
407- }
408-
409- const notFound = b === undefined ? null : b
410- if ( typeof key === 'undefined' || key === '' || key === null ) {
411- if ( engine . allowFunctions || typeof context !== 'function' ) {
412- return context
413- }
414- return null
415- }
416- const subProps = splitPathMemoized ( String ( key ) )
417- for ( let i = 0 ; i < subProps . length ; i ++ ) {
418- if ( context === null || context === undefined ) {
419- return notFound
420- }
421- // Descending into context
422- context = context [ subProps [ i ] ]
423- if ( context === undefined ) {
424- return notFound
425- }
426- }
427- if ( engine . allowFunctions || typeof context !== 'function' ) {
428- return context
429- }
430- return null
431- } ,
432- missing : ( checked , context , above , engine ) => {
433- return ( Array . isArray ( checked ) ? checked : [ checked ] ) . filter ( ( key ) => {
434- return defaultMethods . var ( key , context , above , engine ) === null
435- } )
436- } ,
437- missing_some : ( [ needCount , options ] , context , above , engine ) => {
438- const missing = defaultMethods . missing ( options , context , above , engine )
439- if ( options . length - missing . length >= needCount ) {
440- return [ ]
441- } else {
442- return missing
443- }
444- } ,
445367 map : createArrayIterativeMethod ( 'map' ) ,
446368 some : createArrayIterativeMethod ( 'some' , true ) ,
447369 all : createArrayIterativeMethod ( 'every' , true ) ,
@@ -745,16 +667,7 @@ Object.keys(defaultMethods).forEach((item) => {
745667 ? true
746668 : defaultMethods [ item ] . deterministic
747669} )
748- // @ts -ignore Allow custom attribute
749- defaultMethods . var . deterministic = ( data , buildState ) => {
750- return buildState . insideIterator && ! String ( data ) . includes ( '../../' )
751- }
752- Object . assign ( defaultMethods . missing , {
753- deterministic : false
754- } )
755- Object . assign ( defaultMethods . missing_some , {
756- deterministic : false
757- } )
670+
758671// @ts -ignore Allow custom attribute
759672defaultMethods [ '<' ] . compile = function ( data , buildState ) {
760673 if ( ! Array . isArray ( data ) ) return false
@@ -926,81 +839,11 @@ defaultMethods['!!'].compile = function (data, buildState) {
926839 return `(!!engine.truthy(${ data } ))`
927840}
928841defaultMethods . none . deterministic = defaultMethods . some . deterministic
929- defaultMethods . get . compile = function ( data , buildState ) {
930- let defaultValue = null
931- let key = data
932- let obj = null
933- if ( Array . isArray ( data ) && data . length <= 3 ) {
934- obj = data [ 0 ]
935- key = data [ 1 ]
936- defaultValue = typeof data [ 2 ] === 'undefined' ? null : data [ 2 ]
937-
938- // Bail out if the key is dynamic; dynamic keys are not really optimized by this block.
939- if ( key && typeof key === 'object' ) return false
940-
941- key = key . toString ( )
942- const pieces = splitPathMemoized ( key )
943- if ( ! chainingSupported ) {
944- return `(((a,b) => (typeof a === 'undefined' || a === null) ? b : a)(${ pieces . reduce (
945- ( text , i ) => {
946- return `(${ text } ||0)[${ JSON . stringify ( i ) } ]`
947- } ,
948- `(${ buildString ( obj , buildState ) } ||0)`
949- ) } , ${ buildString ( defaultValue , buildState ) } ))`
950- }
951- return `((${ buildString ( obj , buildState ) } )${ pieces
952- . map ( ( i ) => `?.[${ buildString ( i , buildState ) } ]` )
953- . join ( '' ) } ?? ${ buildString ( defaultValue , buildState ) } )`
954- }
955- return false
956- }
957- // @ts -ignore Allow custom attribute
958- defaultMethods . var . compile = function ( data , buildState ) {
959- let key = data
960- let defaultValue = null
961- if (
962- ! key ||
963- typeof data === 'string' ||
964- typeof data === 'number' ||
965- ( Array . isArray ( data ) && data . length <= 2 )
966- ) {
967- if ( Array . isArray ( data ) ) {
968- key = data [ 0 ]
969- defaultValue = typeof data [ 1 ] === 'undefined' ? null : data [ 1 ]
970- }
971-
972- if ( key === '../index' && buildState . iteratorCompile ) return 'index'
973-
974- // this counts the number of var accesses to determine if they're all just using this override.
975- // this allows for a small optimization :)
976- if ( typeof key === 'undefined' || key === null || key === '' ) return 'context'
977- if ( typeof key !== 'string' && typeof key !== 'number' ) return false
978-
979- key = key . toString ( )
980- if ( key . includes ( '../' ) ) return false
981-
982- const pieces = splitPathMemoized ( key )
983-
984- if ( ! buildState . engine . allowFunctions ) buildState . methods . preventFunctions = a => typeof a === 'function' ? null : a
985- else buildState . methods . preventFunctions = a => a
986-
987- // support older versions of node
988- if ( ! chainingSupported ) {
989- return `(methods.preventFunctions(((a,b) => (typeof a === 'undefined' || a === null) ? b : a)(${ pieces . reduce (
990- ( text , i ) => `(${ text } ||0)[${ JSON . stringify ( i ) } ]` ,
991- '(context||0)'
992- ) } , ${ buildString ( defaultValue , buildState ) } )))`
993- }
994- return `(methods.preventFunctions(context${ pieces
995- . map ( ( i ) => `?.[${ JSON . stringify ( i ) } ]` )
996- . join ( '' ) } ?? ${ buildString ( defaultValue , buildState ) } ))`
997- }
998- return false
999- }
1000842
1001843// @ts -ignore Allowing a optimizeUnary attribute that can be used for performance optimizations
1002- defaultMethods [ '+' ] . optimizeUnary = defaultMethods [ '-' ] . optimizeUnary = defaultMethods . var . optimizeUnary = defaultMethods [ '!' ] . optimizeUnary = defaultMethods [ '!!' ] . optimizeUnary = defaultMethods . cat . optimizeUnary = true
844+ defaultMethods [ '+' ] . optimizeUnary = defaultMethods [ '-' ] . optimizeUnary = defaultMethods [ '!' ] . optimizeUnary = defaultMethods [ '!!' ] . optimizeUnary = defaultMethods . cat . optimizeUnary = true
1003845
1004846export default {
1005- ...defaultMethods
847+ ...defaultMethods ,
848+ ...legacyMethods
1006849}
0 commit comments