Skip to content

Commit

Permalink
component now accepts argument names, previous version is now named c…
Browse files Browse the repository at this point in the history
…omplexComponent
  • Loading branch information
sz-piotr committed Aug 14, 2017
1 parent 6db838c commit 25d5285
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 36 deletions.
18 changes: 3 additions & 15 deletions examples/basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,9 @@

const engine = new Engine()

const PositionComponent = component(function(x, y) {
this.x = x
this.y = y
})

const VelocityComponent = component(function(x, y) {
this.x = x
this.y = y
})

const RectangleComponent = component(function(width, height, color) {
this.width = width
this.height = height
this.color = color
})
const PositionComponent = component('x', 'y')
const VelocityComponent = component('x', 'y')
const RectangleComponent = component('width', 'height', 'color')

const MovementSystem = {
query: Query.all(PositionComponent, VelocityComponent),
Expand Down
2 changes: 1 addition & 1 deletion lib/ouyo.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/ouyo.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"system"
],
"license": "MIT",
"version": "0.3.1",
"version": "0.4.0",
"main": "lib/ouyo.js",
"repository": {
"type": "git",
Expand Down
17 changes: 16 additions & 1 deletion src/component.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
let index = 0

export function component(objectConstructor, objectDestructor) {
export function component() {
let functionArguments = []
let body = ''
for(let i = 0; i < arguments.length; i++) {
if(/^[_a-zA-Z]\w*$/.exec(arguments[i])) {
let arg = arguments[i]
functionArguments[i] = arg
body += `this.${arg}=${arg};`
} else {
throw new Error('Invalid identifier: ' + arguments[i])
}
}
return complexComponent(new Function(functionArguments, body))
}

export function complexComponent(objectConstructor, objectDestructor) {
const id = index++
objectConstructor.id = id
objectConstructor.prototype._id = id
Expand Down
47 changes: 37 additions & 10 deletions src/component.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
import { component } from './component'
import { component, complexComponent } from './component'

describe('component', () => {
test('the result should be a constructor', () => {
const TestComponent = component('a', 'b')
expect(new TestComponent(1, 2)).toEqual({ a: 1, b: 2 })
})

test('the result have and id property', () => {
const TestComponent = component('a', 'b')
const instance = new TestComponent(1, 2)

expect(TestComponent.id).not.toBe(undefined)
expect(TestComponent.id).toEqual(instance._id)
})

test('must work along complexComponent', () => {
const SimpleComponent = component('a', 'b')
const ComplexComponent = complexComponent(
function(a, b) {
this.a = a
this.b = b
}
)
expect(ComplexComponent.id).toEqual(SimpleComponent.id + 1)
expect(SimpleComponent.destroy).toBeInstanceOf(Function)
})
})

describe('complexComponent', () => {
test('should modify the prototype of the argument', () => {
function Undecorated(a, b) {
this.a = a
this.b = b
}
const Decorated = component(Undecorated)
const Decorated = complexComponent(Undecorated)

expect(new Decorated(1, 2)._id).toBe(Decorated.id)
})
Expand All @@ -16,7 +43,7 @@ describe('component', () => {
this.a = a
this.b = b
}
const Decorated = component(Undecorated)
const Decorated = complexComponent(Undecorated)

expect(new Decorated(1, 2)).toEqual({ a: 1, b: 2 })
})
Expand All @@ -26,23 +53,23 @@ describe('component', () => {
this.a = a
this.b = b
}
const DecoratedA = component(Undecorated)
const DecoratedB = component(Undecorated)
const DecoratedA = complexComponent(Undecorated)
const DecoratedB = complexComponent(Undecorated)

expect(DecoratedA).toBe(DecoratedB)
})

test('consecutive calls increment the id', () => {
const DecoratedA = component(function() {})
const DecoratedB = component(function() {})
const DecoratedC = component(function() {})
const DecoratedA = complexComponent(function() {})
const DecoratedB = complexComponent(function() {})
const DecoratedC = complexComponent(function() {})

expect(DecoratedB.id).toEqual(DecoratedA.id + 1)
expect(DecoratedC.id).toBe(DecoratedB.id + 1)
})

test('should allow object destruction', () => {
const Component = component(function (a, b) {
const Component = complexComponent(function (a, b) {
this.a = a
this.b = b
})
Expand All @@ -52,7 +79,7 @@ describe('component', () => {

test('upon destruction calls the second parameter', () => {
let innerInstance
const Component = component(function (a, b) {
const Component = complexComponent(function (a, b) {
this.a = a
this.b = b
}, function() {
Expand Down
6 changes: 3 additions & 3 deletions src/entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Key } from './key'
import { component } from './component'

const noop = () => {}
const ComponentA = component(function() {})
const ComponentB = component(function() {})
const ComponentA = component()
const ComponentB = component()
const idMap = {}
idMap[ComponentA.id] = 0
idMap[ComponentB.id] = 1
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('Entity', () => {
})

test('using unknown component results in exception', () => {
const ComponentC = component(function() {})
const ComponentC = component()
const entity = new Entity(count, idMap, noop)

expect(() => entity.add(new ComponentC())).toThrow()
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { Engine } from './engine'
export { component } from './component'
export { component, complexComponent } from './component'
export { Query } from './query'
6 changes: 3 additions & 3 deletions src/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { component } from './component'
import { Engine } from './engine'
import { Key } from './key'

const ComponentA = component(function() {})
const ComponentB = component(function() {})
const ComponentC = component(function() {})
const ComponentA = component()
const ComponentB = component()
const ComponentC = component()

describe('Query', () => {
test('all should create an object with bake method', () => {
Expand Down

0 comments on commit 25d5285

Please sign in to comment.