New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

resultify

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resultify - npm Package Compare versions

Comparing version 0.2.4 to 1.1.0

__reference.js

156

index.js

@@ -0,66 +1,110 @@

'use strict'
var ResType = require('result-type')
, when = require('when/read')
, Result = require('result')
const promisifyCustom = require('util').promisify.custom
class Result {
constructor (err, data) {
this.err = err
this.data = data
}
}
exports.resultifyP = resultifyPromise
exports.resultifyPromise = resultifyPromise
exports.resultify = resultify
exports.resultifySync = resultifySync
/**
* Teach a node function all about the Result type
* const { resultify } = require('resultify')
* const fs = require('fs')
*
* var readFile = resultify(fs.readFile)
* readFile('/path/to/file.js', 'utf8').then(function(src){
* process.stdout.write(src)
* })
*
* @param {Function} fn must take a callback as its last parameter
* @return {Result}
* const readFile = resultify(fs.readFile)
*
* async function main () {
* const { err, data } = await readFile('my-file')
*
* const { err, data } = await resultify((cb) => {
* fs.readFile('my-file', cb)
* })()
* }
*/
function resultify (original) {
if (typeof original !== 'function') {
throw new Error('original must be a function.')
}
module.exports = function(fn){
return function () {
var result = new Result
var i = arguments.length
if (original[promisifyCustom]) {
const fn = original[promisifyCustom]
if (typeof fn !== 'function') {
throw new Error('util.promisify.custom must be a function.')
}
// scan for Result parameters
while (i--) if (arguments[i] instanceof ResType) {
var args = arguments
var fail = function(e){
result.error(e)
}
var self = this
var next = function(value){
args[i] = value
if (i > 0) return when(args[--i], next, fail)
// call `fn` (apply is slow)
try { switch (args.length) {
case 0: fn.call(self, cb); break;
case 1: fn.call(self, args[0], cb); break;
case 2: fn.call(self, args[0], args[1], cb); break;
case 3: fn.call(self, args[0], args[1], args[2], cb); break;
default:
args[args.length++] = cb
fn.apply(self, args)
} } catch (e) { result.error(e) }
}
args[i].read(next, fail)
return result
}
return resultifyPromise(fn)
}
// call `fn` (apply is slow)
try { switch (arguments.length) {
case 0: fn.call(this, cb); break;
case 1: fn.call(this, arguments[0], cb); break;
case 2: fn.call(this, arguments[0], arguments[1], cb); break;
case 3: fn.call(this, arguments[0], arguments[1], arguments[2], cb); break;
default:
arguments[arguments.length++] = cb
fn.apply(this, arguments)
} } catch (e) { result.error (e) }
function fn (...args) {
return new Promise((resolve) => {
original.call(this, ...args, (err, value) => {
if (err) {
return resolve(new Result(err, null))
}
function cb(error, value){
if (error) result.error(error)
else result.write(value)
}
resolve(new Result(null, value))
})
})
}
return result
}
}
return fn
}
/**
* const { resultifyP } = require('resultify')
*
* const { err, data } = await resultifyP(() => {
* return s3.listBuckets().promise()
* })()
*/
function resultifyPromise (original) {
if (typeof original !== 'function') {
throw new Error('original must be a function.')
}
function fn () {
const p = original.apply(this, arguments)
return p.then(createResolveResult, createRejectResult)
}
return fn
}
/**
* const { resultifySync } = require('resultify')
*
* const { err, data } = await resultifySync(() => {
* return JSON.parse(str)
* })()
*/
function resultifySync (original) {
if (typeof original !== 'function') {
throw new Error('original must be a function.')
}
function fn () {
try {
const ret = original.apply(this, arguments)
return new Result(null, ret)
} catch (err) {
return new Result(err, null)
}
}
return fn
}
function createResolveResult (data) {
return new Result(null, data)
}
function createRejectResult (err) {
return new Result(err, null)
}
{
"name": "resultify",
"version": "0.2.4",
"description": "convert node functions to Result returning ones",
"dependencies": {
"result": "0.3.1",
"result-type": "1.0.0",
"when": "http://github.com/jkroso/when/tarball/3.2.3"
"version": "1.1.0",
"description": "Handle errors with async/await without try/catch.",
"main": "index.js",
"scripts": {
"test": "npr standard && node test/index.js"
},
"devDependencies": {},
"repository": {
"type": "git",
"url": "git://github.com/jkroso/resultify.git"
"dependencies": {},
"binDependencies": {
"standard": "14.3.3"
},
"devDependencies": {
"@pre-bundled/tape": "4.11.0",
"node-fetch": "2.6.0",
"npm-bin-deps": "1.4.2"
},
"author": "Raynos <raynos2@gmail.com>",
"repository": "git://github.com/Raynos/resultify.git",
"homepage": "https://github.com/Raynos/resultify",
"bugs": {
"url": "https://github.com/jkroso/resultify/issues"
"url": "https://github.com/Raynos/resultify/issues",
"email": "raynos2@gmail.com"
},
"keywords": [
"cps",
"result"
"contributors": [
{
"name": "Raynos"
}
],
"author": "Jake Rosoman",
"license": "MIT"
"licenses": [
{
"type": "MIT",
"url": "http://github.com/Raynos/resultify/raw/master/LICENSE"
}
]
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc