Fixed if statement for properties of objects

This commit is contained in:
nicco 2018-02-06 21:15:39 +01:00
parent 7f096099b1
commit 105834ae27
3 changed files with 7 additions and 5 deletions

View File

@ -2,8 +2,7 @@ import { compileBlock } from './compiler'
import { ActionFunction, re, error, options, Part } from './options' import { ActionFunction, re, error, options, Part } from './options'
import { getFromObject, readFileSync } from './util' import { getFromObject, readFileSync } from './util'
import { join } from 'path' import { join } from 'path'
import 'colors' import { computeParts } from './parser'
import { computeParts } from './parser';
export const comment: ActionFunction = html => { export const comment: ActionFunction = html => {
@ -21,7 +20,7 @@ export const comment: ActionFunction = html => {
export const logic: ActionFunction = html => { export const logic: ActionFunction = html => {
const rexp = { const rexp = {
start: new RegExp(`${re.begin}\\${re.if} *\\${re.if_else}?[A-z]\\w*? *${re.ending}`, 'g'), start: new RegExp(`${re.begin}\\${re.if} *\\${re.if_else}?${re.valid_variable} *${re.ending}`, 'g'),
else: new RegExp(`${re.begin} *\\${re.if_else} *${re.ending}`, 'g'), else: new RegExp(`${re.begin} *\\${re.if_else} *${re.ending}`, 'g'),
end: RegExp(`${re.begin} *\\${re.closing_tag} *\\${re.if} *${re.ending}`, 'g'), end: RegExp(`${re.begin} *\\${re.closing_tag} *\\${re.if} *${re.ending}`, 'g'),
} }
@ -65,6 +64,7 @@ export const logic: ActionFunction = html => {
return { return {
parts: [(data: any) => { parts: [(data: any) => {
const ret: any = getFromObject(data, current.variable) const ret: any = getFromObject(data, current.variable)
console.log('IF', typeof ret, ret)
let isTrue: boolean = ret !== undefined && ret !== false && ret !== null && ret !== '' let isTrue: boolean = ret !== undefined && ret !== false && ret !== null && ret !== ''
if (current.inverted) isTrue = !isTrue if (current.inverted) isTrue = !isTrue
@ -118,7 +118,7 @@ export const variables: ActionFunction = html => {
export const loop: ActionFunction = html => { export const loop: ActionFunction = html => {
const rexp = { const rexp = {
start: new RegExp(`${re.begin}\\${re.for} *([A-z]\\w*?) *${re.for_in} *([A-z]\\w*?) *${re.ending}`, 'g'), start: new RegExp(`${re.begin}\\${re.for} *${re.valid_variable} *${re.for_in} *${re.valid_variable} *${re.ending}`, 'g'),
end: RegExp(`${re.begin} *\\${re.closing_tag} *\\${re.for} *${re.ending}`, 'g'), end: RegExp(`${re.begin} *\\${re.closing_tag} *\\${re.for} *${re.ending}`, 'g'),
} }

View File

@ -68,6 +68,7 @@ interface Expressions {
for: string for: string
for_in: string for_in: string
closing_tag: string closing_tag: string
valid_variable: string
} }
export const re: Expressions = { export const re: Expressions = {
@ -81,4 +82,5 @@ export const re: Expressions = {
for: '*', for: '*',
for_in: 'in', for_in: 'in',
closing_tag: '/', closing_tag: '/',
valid_variable: '[A-z](\\w|\\.)*?',
} }

View File

@ -1,5 +1,5 @@
<ul> <ul>
{{* i in arr}} {{* i in arr}}
<li>{{i}}</li> <li>{{? i.test }} {{i}} {{/?}}</li>
{{/*}} {{/*}}
</ul> </ul>