Skip to content

joaquimserafim/simple-once

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-once

the simplest way to call a function once

Build StatusCode Coverage 100%ISC License

API

var once = require('simple-once')

once(function([args]))

Usage

var once = require('simple-once')
var fs = require('fs')

var file = fs.createReadStream('file.txt')

function log () {
  console.log.apply(this, arguments)
}

var cb = once(log)

// cb will be called only once

file.on('data', cb)
file.on('end', cb)
file.on('close', cb)
file.on('error', cb)

loading a function once

var once = require('simple-once')

// file foo.js
var counter = 0

module.exports = once(foo)

function foo (cb) {
  counter++
  cb(null, counter)
}

// file bar.js

var foo = require('./foo')

foo(function (error, result) {
  console.info(result) // should print 1
})

// file barr.js

var foo = require('./foo')

foo(function (error, result) {
  console.info(result) // should print 1
})

with arguments

var once = require('simple-once')

// file foo.js

module.exports = once(foo)

function foo (message, cb) {
  cb(null, message.toUpperCase() + '!')
}

// file bar.js

var foo = require('./foo')

foo('hello world', function (error, result) {
  console.info(result) // should print HELLO WORLD!
})

Development

this projet has been set up with a precommit that forces you to follow a code style, no jshint issues and 100% of code coverage before commit

to run test

npm test

to run jshint

npm run lint

to run code style

npm run style

to run check code coverage

npm run coverage:check

to open the code coverage report

npm run coverage:open

About

the simplest way to call and load a function once

Resources

License

Stars

Watchers

Forks

Packages

No packages published