Giter VIP home page Giter VIP logo

hoek's Introduction

@hapi/hoek

Utility methods for the hapi ecosystem.

hoek is part of the hapi ecosystem and was designed to work seamlessly with the hapi web framework and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out hapi – they work even better together.

This module is not intended to solve every problem for everyone, but rather as a central place to store hapi-specific methods. If you're looking for a general purpose utility module, check out lodash.

Visit the hapi.dev Developer Portal for tutorials, documentation, and support

Useful resources

hoek's People

Contributors

adityabakle avatar adrivanhoudt avatar arb avatar blacksun1 avatar catalint avatar cjihrig avatar danielb2 avatar devinivy avatar dorktobes avatar geek avatar hueniverse avatar iamdoron avatar jarrodyellets avatar jonathansamines avatar jory avatar kanongil avatar lenovouser avatar lloydbenson avatar marcuspoehls avatar marsup avatar mcandre avatar nargonath avatar nathanpower avatar nlf avatar nwhitmont avatar simon-p-r avatar thegoleffect avatar thesillyhippo avatar timcosta avatar watson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hoek's Issues

Deep mapToObject

Hi,

I'd need a deep mapToObject for a feature in joi, like :

[ 'a', 'a.b.c', 'a.b.d', 'a.b.c.d' ]

would become

{
  a: {
    b: {
      c: {
        d: true
      }
    },
    d: true
  }
}

Do you think it's something that might go into hoek ?

This is issue i found while using HAPI but reporting here as well

I think this is happening after i increase the timeout for payload. Default was 10 sec and for my service i had to make it 3 min and after that it comes once a while. I am guessing its waiting for some hanged request where there is no answer and somehow throw this exception.

/node_modules/hapi/node_modules/hoek/lib/index.js:407
throw new Error(msgs.join(' ') || 'Unknown error');
^
Error: Invalid nested use of protect.run()
at Object.exports.assert (/node_modules/hapi/node_modules/hoek/lib/index.js:407:11)
at internals.Protect.run (/node_modules/hapi/lib/protect.js:41:11)
at Object.exports.invoke (/node_modules/hapi/lib/handler.js:378:22)
at process (/node_modules/hapi/lib/request.js:349:17)
at internals.Request._reply (/node_modules/hapi/lib/request.js:374:5)
at timeoutReply (/node_modules/hapi/lib/request.js:290:22)
at Timer.listOnTimeout as ontimeout

Failing test "Hoek #displayStack should return the full call stack for display" (Windows)

When I run npm test, I get a failing test in #displayStack.

Trace:

$ npm test

> [email protected] test c:\Documents and Settings\apenneba\Desktop\src\hoek
> make test-cov

node node_modules/lab/bin/lab -r threshold -t 100


  .................................................x
  ...................................

 1 of 85 tests failed:

  1) Hoek #displayStack  should return the full call stack for display:
     expected 'null (c:\\Documents and Settings\\apenneba\\Desktop\\src\\hoek\\test\\index.js:601:30)' to include 'test/index.js:'
      at c:\Documents and Settings\apenneba\Desktop\src\hoek\test\index.js:602:33


Coverage: 0.00%

Code coverage below threshold: 0.00 < 100
make: *** [test-cov] Error 1
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

System:

$ specs node os
Specs:

specs 0.4
https://github.com/mcandre/specs#readme

npm --version
1.2.17

node --version
v0.10.3

systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
OS Name:                   Microsoft Windows XP Professional
OS Version:                5.1.2600 Service Pack 3 Build 2600

Hoek.merge() should not be destructive

Either Hoek.merge() should not be destructive or the documentation is wrong. My feeling is that it should not be destructive.

> var Hoek = require('hoek');
undefined
> var a = { a: 1 }
undefined
> var b = { b: 2 }
undefined
> var c = Hoek.merge(a,b)
{ a: 1, b: 2 }
> a
{ a: 1, b: 2 }

applyToDefaults Should Handle Undefined options More Gracefully

applyToDefaults should not destroy the defaults object when undefined is passed as the options parameter.

Current functionality:

var defaults = {
    a: 'foo'
};

Hoek.applyToDefaults(defaults, undefined);    // Returns null

Desired / Expected functionality
Hoek should either return the original defaults object or it should return an error and not allow undefined to be passed as an options parameter.

@wpreul what do you think?

allow whitelist options when transforming

hoek.transform blacklists on the keys in the map. what are you thoughts on allowing an option to whitelist?

so if the source has keys not defined in the map, then they just pass through to the result.

add #Hoke.forEach?

Hi, several times found loops like:

for (var i = 0, il = err.details.length; i < il; ++i) {
//...
}

we could make more intuitive/readable using functions, just looking the ".forEach"already know exactly what type of loop is:

var forEach = Hoek.forEach;

forEach(err.details, function(i) {
//...
});

forEach(err.details, (i) => {
//...
});

make any sense?

Plain objects can't be compared.

var a = Object.create(null);
var b = Object.create(null);

a.b = 'c';
b.b = 'c';

Hoek.deepEqual(a, b);

Results in a Object object has no method 'hasOwnProperty' error.

Detect [[Class]] instead of using instanceof

A quick glance through Hoek’s source shows that most of the type detection is done through instanceof, eg. instanceof RegExp, etc.

This means that values from different contexts, eg. RegExps generated from a different realm through Node.js’s vm module would not be detected correctly, as the RegExp constructor is different and thus the instanceof check would fail.

Instead, you should use Object.prototype.toString, by doing something like:

var getClass = Object.prototype.toString;
var className = getClass.call(object);
console.log(className); // "[object RegExp]"

This method instead detects the [[Class]] attribute of objects, which are constant across contexts.

Consider support partial function application

What do you think about including a function to allow partial application of arguments?
I think it could be a useful addition and can PR it if you would like to.

Example use case: currently I'm working on a plug-in where I need pass parameters when using async.series:

async.series([
      f1,
      f2,
      f3],
      callback);

I'd like to do something like the following without having to include lodash or underscore:

async.series([
      Hoek.apply(f1, request),
      Hoek.apply(f2, request),
      Hoek.apply(f3, request)],
      callback);

Thoughts?

Cannot read property 'isImmutable' of null

Discovered during a misadventure with plugin.method with bind set:

This code in lib/index.js is vulnerable to Object.getPrototypeOf returning null:

            var proto = Object.getPrototypeOf(obj);
            if (proto.isImmutable) { // hoek/lib/index.js:45
                newObj = obj;
            }

The crash:

     TypeError: Cannot read property 'isImmutable' of null
      at Object.exports.clone (node_modules/hapi/node_modules/hoek/lib/index.js:45:22)
      at Object.exports.clone (node_modules/hapi/node_modules/hoek/lib/index.js:66:37)
      at Object.exports.clone (node_modules/hapi/node_modules/hoek/lib/index.js:66:37)
      at Object.exports.clone (node_modules/hapi/node_modules/hoek/lib/index.js:66:37)
      at Object.exports.clone (node_modules/hapi/node_modules/hoek/lib/index.js:66:37)
      at Object.exports.clone (node_modules/hapi/node_modules/hoek/lib/index.js:66:37)
      at Object.exports.clone (node_modules/hapi/node_modules/hoek/lib/index.js:66:37)
      at internals.Methods._add (node_modules/hapi/lib/methods.js:50:25)
      at internals.Methods.add (node_modules/hapi/lib/methods.js:23:49)
      at Object.root.method (node_modules/hapi/lib/pack.js:334:34)

I'm not sure what's being closed, there, but it's the _cache property of some object wearing:

  • _allowStale
  • _cache
  • _dispose
  • _itemCount
  • _length
  • _lengthCalculator
  • _lru
  • _lruList
  • _max
  • _maxAge
  • _mru

Would that be from lru-cache?

Description

I think the description of the module should be rephrased as it is not a general purpose utility.

The Hoek general purpose node utilities library is used to aid in a variety of manners.

This line clearly conflicts with the paragraph at the top:

If you're looking for a general purpose utility module, check out lodash or underscore.

applyToDefaults should return defaults when options is undefined

applyToDefaults will return null if options is undefined

> var options = undefined
undefined
> var defaults = { host: 'localhost', port: 10700, options: { cors: true } }
{ host: 'localhost', port: 10700, options: { cors: true } }
> var config = Hoek.applyToDefaults(defaults, options)
null

Hoek.assert - throw error that is passed in as message

If a single error is passed in as the message to a hoek.assert(condition, msg) call, it should just throw that error. That would let us do stuff like:

hoek.assert(condition, Boom.badRequest('bad, bad request'));

and have the Boom error thrown, instead of a generic Error with 'bad, bad request' as its message, which is the current behavior if you do that.

Clone doesn't protect against circular references

ran in to this while implementing domains.

underscore's clone doesn't have this problem, i looked at what they are doing and they reference like 7 other internal functions so it's not super easy to pull out.

Add Bench

Same as Timer but uses process.hrtime() instead of Date.now()

Hoek.reach with negative index

What would you think of accepting negative indexes on Hoek.reach() ?
There's currently no way to take for example the last element of an array.
I know that would involve a bit more complexity but it would make hapijs/joi#507 doable, since reach is kind of hot code, it's up to you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.