How to use hoek - 10 common examples

To help you get started, we’ve selected a few hoek examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / hoek / hoek-tests.ts View on Github external
*/

// clone(obj)

let nestedObj = {
    w: /^something$/ig,
    x: {
        a: [1, 2, 3],
        b: 123456,
        c: new Date()
    },
    y: 'y',
    z: new Date()
};

let copy = Hoek.clone(nestedObj);

copy.x.b = 100;

console.log(copy.y);        // results in 'y'
console.log(nestedObj.x.b); // results in 123456
console.log(copy.x.b);      // results in 100

// cloneWithShallow(obj, keys)

nestedObj = {
    w: /^something$/ig,
    x: {
        a: [1, 2, 3],
        b: 123456,
        c: new Date()
    },
github DefinitelyTyped / DefinitelyTyped / hoek / hoek-tests.ts View on Github external
console.log(copy.x.b);      // results in 100

// merge(target, source, isNullOverride, isMergeArrays)

let target = { a: 1, b: 2 };
let source = { a: 0, c: 5 };
let source2 = { a: null, c: 5 };

Hoek.merge(target, source);         // results in {a: 0, b: 2, c: 5}
Hoek.merge(target, source2);        // results in {a: null, b: 2, c: 5}
Hoek.merge(target, source2, false); // results in {a: 1, b: 2, c: 5}

let targetArray = [1, 2, 3];
let sourceArray = [4, 5];

Hoek.merge(targetArray, sourceArray);              // results in [1, 2, 3, 4, 5]
Hoek.merge(targetArray, sourceArray, true, false); // results in [4, 5]

// applyToDefaults(defaults, options, isNullOverride)

let defaults = { host: "localhost", port: 8000 };
let options = { port: 8080 };

let config = Hoek.applyToDefaults(defaults, options); // results in { host: "localhost", port: 8080 }

defaults = { host: "localhost", port: 8000 };
let options1 = { host: null, port: 8080 };

config = Hoek.applyToDefaults(defaults, options1, true); // results in { host: null, port: 8080 }

// applyToDefaultsWithShallow(defaults, options, keys)
github DefinitelyTyped / DefinitelyTyped / hoek / hoek-tests.ts View on Github external
});
// Results in
// {
//     person: {
//         address: {
//             lineOne: '123 main street',
//             lineTwo: 'PO Box 1234',
//             region: 'CA'
//         }
//     },
//     title: 'Warehouse'
// }

// shallow(obj)

let shallow = Hoek.shallow({ a: { b: 1 } });

// stringify(obj)

let a: any = {};
a.b = a;
Hoek.stringify(a);      // Returns '[Cannot display object: Converting circular structure to JSON]'

// Timer

let timerObj = new Hoek.Timer();
console.log("Time is now: " + timerObj.ts);
console.log("Elapsed time from initialization: " + timerObj.elapsed() + 'milliseconds');

// Bench

let benchObj = new Hoek.Bench();
github DefinitelyTyped / DefinitelyTyped / hoek / hoek-tests.ts View on Github external
let stack = Hoek.displayStack();
console.log(stack);

// callStack(slice)

let stack2 = Hoek.callStack();
console.log(stack2);

// nextTick(fn)

let myFn = () => {
    console.log('Do this later');
};

let nextFn = Hoek.nextTick(myFn);

nextFn();
console.log('Do this first');

// Results in:
//
// Do this first
// Do this later

// once(fn)

myFn = () => {
    console.log('Ran myFn');
};

let onceFn = Hoek.once(myFn);
github outmoded / oz / test / client.js View on Github external
it('errors on reissue fail', async () => {

                const mock = new internals.Mock({ ttl: 10 });
                const uri = await mock.start();

                const connection = new Oz.client.Connection({ uri, credentials: internals.app });
                const { result: result1, code: code1, ticket: ticket1 } = await connection.app('/');
                expect(result1).to.equal('GET /');
                expect(code1).to.equal(200);
                expect(ticket1).to.equal(connection._appTicket);

                await Hoek.wait(11);        // Expire ticket

                let count = 0;
                const orig = Wreck.request;
                Wreck.request = function (...args) {

                    if (++count === 1) {
                        return orig.apply(Wreck, args);
                    }

                    Wreck.request = orig;
                    return Promise.reject(new Error('bad socket'));
                };

                await expect(connection.request('/resource', ticket1, { method: 'POST' })).to.reject();
                await mock.stop();
            });
github DefinitelyTyped / DefinitelyTyped / hoek / hoek-tests.ts View on Github external
nextFn();
console.log('Do this first');

// Results in:
//
// Do this first
// Do this later

// once(fn)

myFn = () => {
    console.log('Ran myFn');
};

let onceFn = Hoek.once(myFn);
onceFn(); // results in "Ran myFn"
onceFn(); // results in undefined

// ignore

Hoek.ignore();

// uniqueFilename(path, extension)

let result1 = Hoek.uniqueFilename('./test/modules', 'txt'); // results in "full/path/test/modules/{random}.txt"

// isInteger(value)

result = Hoek.isInteger('23');
github DefinitelyTyped / DefinitelyTyped / hoek / hoek-tests.ts View on Github external
// stringify(obj)

let a: any = {};
a.b = a;
Hoek.stringify(a);      // Returns '[Cannot display object: Converting circular structure to JSON]'

// Timer

let timerObj = new Hoek.Timer();
console.log("Time is now: " + timerObj.ts);
console.log("Elapsed time from initialization: " + timerObj.elapsed() + 'milliseconds');

// Bench

let benchObj = new Hoek.Bench();
console.log("Elapsed time from initialization: " + benchObj.elapsed() + 'milliseconds');

// base64urlEncode(value)

Hoek.base64urlEncode("hoek");

// base64urlDecode(value)

Hoek.base64urlDecode("aG9law==");

// escapeHtml(string)

let string = ' hey ';
let escapedString = Hoek.escapeHtml(string); // returns <html> hey </html>

// escapeHeaderAttribute(attribute)
github hapijs / bell / test / providers.js View on Github external
server.register(Bell, function (err) {

                    expect(err).to.not.exist();

                    var custom = Bell.providers.bitbucket();
                    Hoek.merge(custom, provider);

                    Mock.override('https://bitbucket.org/api/1.0/user', {
                        // source: https://confluence.atlassian.com/display/BITBUCKET/user+Endpoint
                        repositories: [{}],
                        user: {
                            first_name: 'Steve',
                            last_name: '',
                            username: 'steve_stevens'
                        }
                    });

                    server.auth.strategy('custom', 'bell', {
                        password: 'password',
                        isSecure: false,
                        clientId: 'twitter',
                        clientSecret: 'secret',
github DefinitelyTyped / DefinitelyTyped / hoek / hoek-tests.ts View on Github external
let newArray1 = Hoek.unique(array1, "id");  // results in [{id: 1}, {id: 2}]

// mapToObject(array, key)

array = [1, 2, 3];
let newObject = Hoek.mapToObject(array);   // results in {"1": true, "2": true, "3": true}

array1 = [{ id: 1 }, { id: 2 }];
newObject = Hoek.mapToObject(array1, "id"); // results in {"1": true, "2": true}

// intersect(array1, array2)

array = [1, 2, 3];
let array2 = [1, 4, 5];

let newArray2 = Hoek.intersect(array, array2); // results in [1]

// contain(ref, values, [options])

Hoek.contain('aaa', 'a', { only: true });                           // true
Hoek.contain([{ a: 1 }], [{ a: 1 }], { deep: true });               // true
Hoek.contain([1, 2, 2], [1, 2], { once: true });                    // false
Hoek.contain({ a: 1, b: 2, c: 3 }, { a: 1, d: 4 }, { part: true }); // true

// flatten(array, [target])

let array3 = [1, [2, 3]];

let flattenedArray = Hoek.flatten(array); // results in [1, 2, 3]

array3 = [1, [2, 3]];
let target1 = [4, [5]];
github DefinitelyTyped / DefinitelyTyped / hoek / hoek-tests.ts View on Github external
config = Hoek.applyToDefaults(defaults, options1, true); // results in { host: null, port: 8080 }

// applyToDefaultsWithShallow(defaults, options, keys)

let defaults1 = {
    server: {
        host: "localhost",
        port: 8000
    },
    name: 'example'
};

let options2 = { server: { port: 8080 } };

let config1 = Hoek.applyToDefaultsWithShallow(defaults1, options2, ['server']); // results in { server: { port: 8080 }, name: 'example' }

// deepEqual(b, a, [options])

Hoek.deepEqual({ a: [1, 2], b: 'string', c: { d: true } }, { a: [1, 2], b: 'string', c: { d: true } }); //results in true
Hoek.deepEqual(Object.create(null), {}, { prototype: false }); //results in true
Hoek.deepEqual(Object.create(null), {}); //results in false

// unique(array, key)

let array = [1, 2, 2, 3, 3, 4, 5, 6];

let newArray = Hoek.unique(array);    // results in [1,2,3,4,5,6]

let array1 = [{ id: 1 }, { id: 1 }, { id: 2 }];

let newArray1 = Hoek.unique(array1, "id");  // results in [{id: 1}, {id: 2}]