Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
roboncode committed Sep 3, 2018
0 parents commit 7974cc0
Show file tree
Hide file tree
Showing 30 changed files with 9,233 additions and 0 deletions.
68 changes: 68 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/
docker/
dist/
tmp/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

# editor settings
.vscode
.idea
12 changes: 12 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.DS_Store
.vscode/
.idea/
docker/
docs/
examples/
node_modules/
test/
tools/
wallaby.js
.travis.yml
*.log
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sudo: required

language: node_js
node_js:
- 8.11.4

after_success: npm run coverage
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gem 'coveralls', require: false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Rob Taylor

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Tang Object Modeling built on top of [Joi](https://github.com/hapijs/joi)


## MIT License

Copyright (c) 2018 RobOnCode <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Export lib/tang
*
*/

module.exports = require('./lib/');
111 changes: 111 additions & 0 deletions lib/Builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const asyncForEach = require('./helpers/asyncForEach')
const microtime = require('microtime')
const snooze = require('./helpers/snooze')
const padding = 35
require('colors')

class Builder {
static getInstance(name = 'default') {
if (!this._instances) {
this._instances = {}
}
if (!this._instances[name]) {
this._instances[name] = new this()
}
return this._instances[name]
}

constructor() {
this.methods = {}

this.addMethod('convertTo', function(data, index, items, Model) {
if(Model === data.constructor) {
return data
}
return new Model(data)
})

this.addMethod('toObject', function(model, index, items, options) {
if (model.toObject) {
return model.toObject(options)
}
throw new Error('toObject() requires first element to be of type Model')
})

this.addMethod('inspect', function(target, index, items, note = 'Inspect') {
console.log(note, `[${index}] =>`, target)
return target
})

this.addMethod('intercept', function(target, index, items, callback) {
return callback(target, index || 0)
})
}

data(data) {
this.isArray = data instanceof Array
this.items = [].concat(data)
this.queue = []
return this
}

addMethod(name, method) {
this[name] = function() {
let args = [].slice.call(arguments)
this.queue.push({
method,
args
})
return this
}
}

async build(handler) {
let report = []
let startTotalTime = microtime.now()
let startTime
let items = this.items
let item = await asyncForEach(items, async (item, index) => {
if (this.queue.length) {
items[index] = await asyncForEach(this.queue, async message => {
if (handler) {
startTime = microtime.now()
}
let args = [].concat(item, index, [items], message.args)
try {
await snooze()
item = await message.method.apply(this, args)
if (handler) {
report.push(
('item[' + index + '].' + message.method + ': ').padEnd(
padding
) +
(microtime.now() - startTime) * 0.001
)
}
return item
} catch (e) {
console.log(`ValidationError: ${e.message}`.bgRed)
return e
}
})
return items[index]
}
return item
})
if (handler) {
report.push(
'totalTime:'.padEnd(padding) +
(microtime.now() - startTotalTime) * 0.001 +
' ms'
)
handler(report.join('\n'))
}
if (this.isArray) {
return this.items
}
return item
}
}

module.exports = Builder
16 changes: 16 additions & 0 deletions lib/EventDispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const EventEmitter = require('events')


class EventDispatcher {
static getInstance(name = 'default') {
if (!this.eventEmitters) {
this.eventEmitters = {}
}
if (!this.eventEmitters[name]) {
this.eventEmitters[name] = new EventEmitter()
}
return this.eventEmitters[name]
}
}

module.exports = EventDispatcher
Loading

0 comments on commit 7974cc0

Please sign in to comment.