Skip to content

Instantly share code, notes, and snippets.

@crizstian
Last active January 12, 2025 07:50
Show Gist options
  • Save crizstian/ba39c6fdbb30a40c738e3c07ea83b5c1 to your computer and use it in GitHub Desktop.
Save crizstian/ba39c6fdbb30a40c738e3c07ea83b5c1 to your computer and use it in GitHub Desktop.

Revisions

  1. crizstian revised this gist Jan 9, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions solid.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /*
    Code examples from the article:
    Code examples from the article: S.O.L.I.D The first 5 priciples of Object Oriented Design with JavaScript
    https://medium.com/@cramirez92/s-o-l-i-d-the-first-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa#.7uj4n7rsa
    */

    const shapeInterface = (state) => ({
  2. crizstian created this gist Jan 9, 2017.
    127 changes: 127 additions & 0 deletions solid.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,127 @@
    /*
    Code examples from the article:
    */

    const shapeInterface = (state) => ({
    type: 'shapeInterface',
    area: () => state.area(state)
    })

    const solidShapeInterface = (state) => ({
    type: 'solidShapeInterface',
    volume: () => state.volume(state)
    })

    const manageShapeInterface = (fn) => ({
    type: 'manageShapeInterface',
    calculate: () => fn()
    })

    const square = (length) => {
    const proto = {
    length,
    type : 'Square',
    area : (args) => Math.pow(args.length, 2)
    }
    const basics = shapeInterface(proto)
    const abstraccion = manageShapeInterface(() => basics.area())
    const composite = Object.assign({}, basics, abstraccion)
    return Object.assign(Object.create(composite), {length})
    }

    const circle = (radius) => {
    const proto = {
    radius,
    type: 'Circle',
    area: (args) => Math.PI * Math.pow(args.radius, 2)
    }

    const basics = shapeInterface(proto)
    const abstraccion = manageShapeInterface(() => basics.area())
    const composite = Object.assign({}, basics, abstraccion)
    return Object.assign(Object.create(composite), {radius})
    }

    const cubo = (length) => {
    const proto = {
    length,
    type : 'Cubo',
    area : (args) => Math.pow(args.length, 2),
    volume : (args) => Math.pow(args.length, 3)
    }
    const basics = shapeInterface(proto)
    const complex = solidShapeInterface(proto)
    const abstraccion = manageShapeInterface(() => basics.area() + complex.volume())
    const composite = Object.assign({}, basics, abstraccion)
    return Object.assign(Object.create(composite), {length})
    }

    const areaCalculator = (s) => {
    const proto = {
    type: 'areaCalculator',
    sum() {
    const area = []
    for (shape of this.shapes) {
    area.push(shape.calculate())
    }
    return area.reduce((v, c) => c += v, 0)
    }
    }

    return Object.assign(Object.create(proto), {shapes: s})
    }

    const volumeCalculator = (s) => {
    const proto = {
    type: 'volumeCalculator'
    }
    const areaCalProto = Object.getPrototypeOf(areaCalculator())
    const inherit = Object.assign({}, areaCalProto, proto)
    return Object.assign(Object.create(inherit), {shapes: s})
    }

    const sumCalculatorOputter = (a) => {
    const proto = {
    JSON() {
    return JSON.stringify(this.calculator.sum())
    },
    HAML() {
    return `HAML format output`
    },
    HTML() {
    return `
    <h1>
    Sum of the areas of provided shapes:
    ${this.calculator.sum()}
    </h1>`
    },
    JADE() {
    return `JADE format output`
    }
    }
    return Object.assign(Object.create(proto), {calculator: a})
    }

    const shapes = [
    circle(2),
    square(5),
    square(6)
    ]

    const solids = [
    cubo(4)
    ]

    const areas = areaCalculator(shapes)
    const volume = volumeCalculator(solids)

    const output = sumCalculatorOputter(areas)
    const output2 = sumCalculatorOputter(volume)

    console.log(output.JSON())
    console.log(output.HAML())
    console.log(output.HTML())
    console.log(output2.HTML())
    console.log(output.JADE())