Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben-Arushanyan committed Oct 17, 2022
1 parent 5bfbe68 commit 59b0fc8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/components/if-else/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import React, {Children} from 'react'
import React from 'react'
import {ComponentChildren} from '../../services/ComponentChildren'

const If = (props) => {
let {children, condition} = props
condition = !!condition
children = Children.toArray(children)
const {children, condition} = props
const _children = new ComponentChildren(children)
if (condition) {
children = children.filter(node => (node.type !== Else) && (node.type !== ElseIf))
_children.removeByNodeTypes([Else, ElseIf])
} else {
children = children.filter(node => (node.type === Else) || (node.type === ElseIf))
_children.removeExceptNodeTypes([Else, ElseIf])
}
return children
_children.callFunctions()
return children.value
}

const Else = (props) => {
let {children} = props
return children
const {children} = props
const _children = new ComponentChildren(children)
_children.callFunctions()
return _children.value
}

const ElseIf = (props) => <If {...props} />
Expand Down
46 changes: 46 additions & 0 deletions src/services/ComponentChildren.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {isArray} from './utils'
import {ElementNode} from './ElementNode'

class ComponentChildren {
#value
constructor(children) {
this.#value = isArray(children) ? children : [children]
}

get value() {
const length = this.#value.length
if (length) {
if (length === 1) {
return this.#value[0]
}
return this.#value
}
return null
}

callFunctions() {
this.#value = this.#value.map(node => {
node = new ElementNode(node)
node.callFunction()
return node.value
})
}

removeByNodeTypes(types) {
this.#value = this.#value.filter(el => {
const node = new ElementNode(el)
const nodeType = node.getType()
return !types.include(nodeType)
})
}

removeExceptNodeTypes(types) {
this.#value = this.#value.filter(el => {
const node = new ElementNode(el)
const nodeType = node.getType()
return types.include(nodeType)
})
}
}

export {ComponentChildren}
18 changes: 18 additions & 0 deletions src/services/ElementNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {isObject, isFunction} from './utils'

class ElementNode {
value
constructor(node) {
this.value = node
}

getType() {
return isObject(this.value) ? this.value.type : null
}

callFunction() {
this.value = isFunction(this.value) ? this.value() : this.value
}
}

export {ElementNode}
3 changes: 2 additions & 1 deletion src/services/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
export const eq = (x, y) => x === y || (x !== x && y !== y)
export const isFunction = (x) => typeof x === 'function'
export const isObject = (x) => x !== null && (typeof x === 'object' || typeof x === 'function')
export const has = (x, key) => x !== null && Object.prototype.hasOwnProperty.call(x, key)
export const has = (x, key) => x !== null && Object.prototype.hasOwnProperty.call(x, key)
export const isArray = x => Array.isArray(x)

0 comments on commit 59b0fc8

Please sign in to comment.