Skip to content

Commit

Permalink
2.0.0-beta.8: Completely rewritten from the ground up
Browse files Browse the repository at this point in the history
  • Loading branch information
Download committed Nov 14, 2020
1 parent 423ce1c commit 36506de
Show file tree
Hide file tree
Showing 83 changed files with 5,354 additions and 5,827 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
node_modules
npm-debug.log*
/dist
/node_modules
/ulog.min.js
/.nyc_output
/test.min.js
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

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) 2020 Stijn de Witt

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.
161 changes: 0 additions & 161 deletions LICENSE.md

This file was deleted.

712 changes: 416 additions & 296 deletions README.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion _config.yml

This file was deleted.

22 changes: 0 additions & 22 deletions browser.js

This file was deleted.

27 changes: 27 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var fs = require('fs')
var path = require('path')

var gzipSize = require('gzip-size')
// be cool and use ulog to print the logging in the build of ulog :)
var log = require('./')('ulog:build')

var [ processName, script, command, ...args ] = process.argv
var pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
var v = pkg.version

;(function(){
var data = fs.readFileSync(path.resolve(__dirname, pkg.unpkg), 'utf8')
var gzip = (gzipSize.sync(data) / 1024).toFixed(1)
log.info(`Built ${pkg.unpkg} (~${gzip} kB minified and gzipped)`)
data = fs.readFileSync(path.resolve(__dirname, 'full.min.js'), 'utf8')
var fullzip = (gzipSize.sync(data) / 1024).toFixed(1)
log.info(`Built full.min.js (~${fullzip} kB minified and gzipped)`)
var readme = fs.readFileSync('README.md', 'utf-8')
readme = readme.replace(/ulog@\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?/g, `ulog@${v}`)
readme = readme.replace(/\<sub\>\<sup\>v\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?/g, `<sub\><sup\>v${v}`)
readme = readme.replace(/ulog@\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?\) \(~\d\.\dkB/g, `ulog@${v}) (~${gzip}kB`)
readme = readme.replace(/just 2.7kB/g, `just ${gzip}kB`)
readme = readme.replace(/ulog@\d(\d)?\.\d(\d)?\.\d(\d)?(-[a-z]+\.\d(\d)?)?\/full\.min\.js\) \(~\d\.\dkB/g, `ulog@${v}/full.min.js) (~${fullzip}kB`)
fs.writeFileSync('README.md', readme, 'utf8')
log.info(`Updated README.md`)
})()
28 changes: 0 additions & 28 deletions build/build-umd.js

This file was deleted.

12 changes: 12 additions & 0 deletions core/grab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = function(ulog, name, result) {
ulog.mods.reduce(function(r,item){
if (Array.isArray(r) && (name in item)) {
r.push(item[name])
} else {
for (var o in item[name])
r[o] = item[name][o]
}
return r
}, result)
return result
}
111 changes: 111 additions & 0 deletions core/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
var ulog = require('anylogger')
var grab = require('./grab')

// ulog.levels.none = 0
// ulog.levels.all = 9007199254740991 // Number.MAX_SAFE_INTEGER
// var ext = ulog.ext // save for later

/**
* `ulog.ext(logger) => logger`
*
* Called when a logger needs to be extended, either because it was newly
* created, or because it's configuration or settings changed in some way.
*
* This method must ensure that a log method is available on the logger
* for each level in `ulog.levels`.
*
* This override calls `ext` on all mods when a logger is extended and
* enables calling ext on all loggers by passing no arguments.
*/
ulog.ext = function(logger) {
if (logger) {
// ext(logger) // create default methods
grab(ulog, 'ext', []).map(function(ext){
ext.call(ulog, logger) // call hook on registered mods
})
return logger
} else {
for (logger in ulog()) {
ulog.ext(ulog(logger))
}
}
}

ulog.mods = []

/**
* ### `ulog.use(mod: Object|Array<Object>): Number`
*
* Makes ulog use `mod`.
*
* The mod(s) is/are added to `ulog.mods`. This function checks whether `mod`
* is already in use and only adds it if needed. Checks whether `mod` has a key
* `use` containing mods `mod` depends on and adding those first, guaranteeing
* the order in which mods are added. Returns the total number of mods added,
* including transitive dependencies.
*
* @param mod A single mod object or an array of mod objects.
* @returns The number of mods that were added
*
* E.g.:
* ```
* var mod = require('./mod')
* var solo = {} // the simplest mod is just an empty object
* var using = {
* // you can declare a dependency on other mods
* use: [
* mod
* ]
* }
*
* ulog.add(solo) // returns 1
* ulog.add(solo) // returns 0, because mods are only added once
* ulog.add(using) // returns 2, because `using` depends on `mod`
* ulog.add(mod) // returns 0, because `mod` was already added by `using`
* ```
*/
ulog.use = function(mod) {
// handle mod being an array of mods
if (Array.isArray(mod)) {
return mod.reduce(function(r,mod){return r + ulog.use(mod)}, 0)
}
// handle mod being a single mod
var result = ulog.mods.indexOf(mod) === -1 ? 1 : 0
if (result) {
if (mod.use) {
// use dependencies
result += ulog.use(mod.use)
}
if (mod.extend) {
for (var name in mod.extend) {
ulog[name] = mod.extend[name]
}
}
if (mod.init) {
mod.init.call(ulog)
}
ulog.mods.push(mod)
}
return result
}

// ulog.grab = function(name){
// return ulog.mods.reduce(function(r,mod){
// for (var o in mod[name]) {
// r[o] = mod[name][o]
// }
// return r
// }, {})
// }

// var recorders = []
// for (var i=0,mod; mod=ulog.mods[i]; i++) {
// if (mod.record) recorders.push(mod.record)
// }


// ulog.enabled = ulog.get.bind(ulog, 'debug')
// ulog.enable = ulog.set.bind(ulog, 'debug')
// ulog.disable = ulog.set.bind(ulog, 'debug', undefined)

module.exports = ulog
136 changes: 136 additions & 0 deletions core/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
var test = require('tape')
var sinon = require('sinon');

var ulog = require('./')

test('ulog(name?: String, options?: Object): Object|Function', function(t) {
t.equal(typeof ulog, 'function', 'is a function')
t.ok('mods' in ulog, 'has a property `mods`')
t.equal(typeof ulog.ext, 'function', 'has a method `ext()`')
t.equal(typeof ulog.use, 'function', 'has a method `use()`')
t.equal(typeof ulog('test'), 'function', 'returns a Logger when called with a name as argument')
t.ok(typeof ulog() === 'object' && ('test' in ulog()), 'returns an object containing entries for all loggers when called without arguments')
delete ulog().test

t.test('ulog.mods', function(t){
t.ok(Array.isArray(ulog.mods), 'is an array')
t.end()
})

t.test('ulog.ext(logger: Function): Function', function(t){
var mod = { ext: sinon.stub() }
ulog.use(mod)
var logger = ulog('test')
t.equal(mod.ext.callCount, 1, 'calls the `ext` handler on all registered mods')
delete ulog().test
ulog.mods.splice(0, ulog.mods.length)
t.end()
})

t.test('ulog.use(mod: Object|Array): Number', function (t) {
var mod = {}
var result = ulog.use(mod)
t.equal(ulog.mods.indexOf(mod), 0, 'adds single mods to `ulog.mods`')
t.equal(result, 1, 'returns 1 to indicate a single mod was added')
var result = ulog.use(mod)
t.equal(ulog.mods.length, 1, 'does not add the same mod twice')
t.equal(result, 0, 'returns 0 to indicate no mods were added')
ulog.mods.splice(0, ulog.mods.length)

var anotherMod = {}
result = ulog.use([ mod, anotherMod ])
t.equal(ulog.mods.length, 2, 'adds arrays with mods to `ulog.mods`')
t.equal(result, 2, 'returns 2 to indicate two mods were added')
ulog.mods.splice(0, ulog.mods.length)

var dependency = {}
var modWithDeps = {
use: [ dependency ]
}
result = ulog.use(modWithDeps)
t.equal(ulog.mods.length, 2, 'adds dependencies for mods that have a `use` property')
t.equal(ulog.mods.indexOf(dependency), 0, 'adds dependencies before dependants')
t.equal(result, 2, 'returns 2 to indicate two mods were added')
ulog.mods.splice(0, ulog.mods.length)

var modWithExtensions = {
extend: {
test: 'test'
}
}
ulog.use(modWithExtensions)
t.equal(ulog.test, 'test', 'extends ulog for mods that have a `extend` property')
ulog.mods.splice(0, ulog.mods.length)

// ulog.add('mods', { stub: { add: sinon.stub() } })
// t.equal(ulog.mods.stub.add.callCount, 1, 'when adding a mod with an `add()` method, that method is called')
// ulog.add('whatever', { c:'component', b:'another', a:'yet another' })
// t.equal(ulog.mods.stub.add.callCount, 2, 'when adding components, mods with an `add()` method are called')
// delete ulog.whatever
// delete ulog.mods.stub
t.end()
})

// t.test('ulog.set(name: String, value: String)', function (t) {
// var mod = { set: sinon.spy() }
// ulog.add(mod)

// ulog.set('whatever', 'test')
// t.ok(mod.set.called, 'calls set on all registered mods')

// ulog.mods.splice(0, ulog.mods.length)
// t.end()
// })

// t.test('ulog.get(name: String, loggerName: String)', function (t) {
// var mod = { get: sinon.spy() }
// ulog.add(mod)

// ulog.get('whatever')
// t.ok(mod.get.called, 'calls get on all registered mods')

// ulog.mods.splice(0, ulog.mods.length)
// t.end()
// })

t.end()
})

// test('Logger(level?: String = \'log\', ...args)', function(t) {
// var log = ulog('test')
// t.equal(typeof log, 'function', 'is a function')
// t.equal(log.name, 'test', 'has a property `name` that matches the given `name`')
// t.equal(typeof log.error, 'function', 'has a method `error()`')
// t.equal(typeof log.warn, 'function', 'has a method `warn()`')
// t.equal(typeof log.info, 'function', 'has a method `info()`')
// t.equal(typeof log.log, 'function', 'has a method `log()`')
// t.equal(typeof log.debug, 'function', 'has a method `debug()`')
// t.equal(typeof log.trace, 'function', 'has a method `trace()`')
// var sandbox = sinon.createSandbox()
// sandbox.stub(log, 'error')
// sandbox.stub(log, 'warn')
// sandbox.stub(log, 'info')
// sandbox.stub(log, 'log')
// sandbox.stub(log, 'debug')
// sandbox.stub(log, 'trace')
// try {
// log('a log message')
// t.equal(log.log.callCount, 1, 'when called without a level name as first argument, calls Logger.log()')
// log('error', 'an error message')
// t.equal(log.error.callCount, 1, 'when called with \'error\' as first argument, calls Logger.error()')
// log('warn', 'a warning message')
// t.equal(log.warn.callCount, 1, 'when called with \'warn\' as first argument, calls Logger.warn()')
// log('info', 'an info message')
// t.equal(log.info.callCount, 1, 'when called with \'info\' as first argument, calls Logger.info()')
// log('log', 'a log message')
// t.equal(log.log.callCount, 2, 'when called with \'log\' as first argument, calls Logger.log()')
// log('debug', 'a debug message')
// t.equal(log.debug.callCount, 1, 'when called with \'debug\' as first argument, calls Logger.debug()')
// log('trace', 'a trace message')
// t.equal(log.trace.callCount, 1, 'when called with \'trace\' as first argument, calls Logger.trace()')
// }
// finally {
// sandbox.restore()
// }
// t.end()
// })
Loading

0 comments on commit 36506de

Please sign in to comment.