Skip to content

Commit

Permalink
Remove unused methods. Closes #268
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Nov 1, 2018
1 parent ce284c5 commit c1815d3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 547 deletions.
74 changes: 0 additions & 74 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@
* [applyToDefaults](#applytodefaultsdefaults-options-isnulloverride "applyToDefaults")
* [applyToDefaultsWithShallow](#applytodefaultswithshallowdefaults-options-keys "applyToDefaultsWithShallow")
* [deepEqual](#deepequalb-a-options "deepEqual")
* [unique](#uniquearray-key "unique")
* [mapToObject](#maptoobjectarray-key "mapToObject")
* [intersect](#intersectarray1-array2 "intersect")
* [contain](#containref-values-options "contain")
* [flatten](#flattenarray-target "flatten")
* [reach](#reachobj-chain-options "reach")
* [reachTemplate](#reachtemplateobj-template-options "reachTemplate")
* [transform](#transformobj-transform-options "transform")
* [shallow](#shallowsource "shallow")
* [stringify](#stringifyobj "stringify")
* [Bench](#bench "Bench")
* [Escaping Characters](#escaping-characters "Escaping Characters")
Expand Down Expand Up @@ -170,34 +166,6 @@ Hoek.deepEqual(Object.create(null), {}, { prototype: false }); //results in true
Hoek.deepEqual(Object.create(null), {}); //results in false
```

### unique(array, key)

Remove duplicate items from Array

```javascript

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

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

array = [{id: 1}, {id: 1}, {id: 2}];

newArray = Hoek.unique(array, "id"); // results in [{id: 1}, {id: 2}]
```

### mapToObject(array, key)

Convert an Array into an Object

```javascript

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

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

### intersect(array1, array2)

Find the common unique items in two arrays
Expand Down Expand Up @@ -293,48 +261,6 @@ var obj = {a : {b : { c : 1}}};
Hoek.reachTemplate(obj, '1+{a.b.c}=2'); // returns '1+1=2'
```

### transform(obj, transform, [options])

Transforms an existing object into a new one based on the supplied `obj` and `transform` map. `options` are the same as the `reach` options. The first argument can also be an array of objects. In that case the method will return an array of transformed objects. Note that `options.separator` will be respected for the keys in the transform object as well as values.

```javascript
var source = {
address: {
one: '123 main street',
two: 'PO Box 1234'
},
title: 'Warehouse',
state: 'CA'
};

var result = Hoek.transform(source, {
'person.address.lineOne': 'address.one',
'person.address.lineTwo': 'address.two',
'title': 'title',
'person.address.region': 'state'
});
// Results in
// {
// person: {
// address: {
// lineOne: '123 main street',
// lineTwo: 'PO Box 1234',
// region: 'CA'
// }
// },
// title: 'Warehouse'
// }
```

### shallow(source)

Returns a new object with shallow copies of all source properties where:
- `source` - the source to be copied.

```javascript
var shallow = Hoek.shallow({ a: { b: 1 } });
```

### stringify(obj)

Converts an object to string using the built-in `JSON.stringify()` method with the difference that any errors are caught
Expand Down
19 changes: 4 additions & 15 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
![hoek Logo](https://raw.github.com/hapijs/hoek/master/images/hoek.png)

Utility methods for the hapi ecosystem. 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](https://github.com/lodash/lodash) or [underscore](https://github.com/jashkenas/underscore).
Utility methods for the hapi ecosystem. 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](https://github.com/lodash/lodash) or
[underscore](https://github.com/jashkenas/underscore).

[![Build Status](https://secure.travis-ci.org/hapijs/hoek.svg)](http://travis-ci.org/hapijs/hoek)

Lead Maintainer: [Gil Pedersen](https://github.com/kanongil)

## Usage

The *Hoek* library contains some common functions used within the hapi ecosystem. It comes with useful methods for Arrays (clone, merge, applyToDefaults), Objects (removeKeys, copy), Asserting and more.

For example, to use Hoek to set configuration with default options:
```javascript
const Hoek = require('hoek');

const default = {url : "www.github.com", port : "8000", debug : true};

const config = Hoek.applyToDefaults(default, {port : "3000", admin : true});

// In this case, config would be { url: 'www.github.com', port: '3000', debug: true, admin: true }
```

## Documentation

Expand Down
127 changes: 21 additions & 106 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,78 +371,43 @@ exports.deepEqual = function (obj, ref, options, seen) {
};


// Remove duplicate items from array
// Find the common unique items in two arrays

exports.unique = (array, key) => {
exports.intersect = function (array1, array2, justFirst) {

let result;
if (key) {
result = [];
const index = new Set();
array.forEach((item) => {
if (!array1 ||
!array2) {

const identifier = item[key];
if (!index.has(identifier)) {
index.add(identifier);
result.push(item);
}
});
}
else {
result = Array.from(new Set(array));
return (justFirst ? null : []);
}

return result;
};


// Convert array into object

exports.mapToObject = function (array, key) {

if (!array) {
return null;
}
const common = [];
const hash = (Array.isArray(array1) ? new Set(array1) : array1);
const found = new Set();
for (const value of array2) {
if (internals.has(hash, value) &&
!found.has(value)) {

const obj = {};
for (let i = 0; i < array.length; ++i) {
if (key) {
if (array[i][key]) {
obj[array[i][key]] = true;
if (justFirst) {
return value;
}
}
else {
obj[array[i]] = true;

common.push(value);
found.add(value);
}
}

return obj;
return (justFirst ? null : common);
};


// Find the common unique items in two arrays

exports.intersect = function (array1, array2, justFirst) {

if (!array1 || !array2) {
return [];
}

const common = [];
const hash = (Array.isArray(array1) ? exports.mapToObject(array1) : array1);
const found = {};
for (let i = 0; i < array2.length; ++i) {
if (hash[array2[i]] && !found[array2[i]]) {
if (justFirst) {
return array2[i];
}
internals.has = function (ref, key) {

common.push(array2[i]);
found[array2[i]] = true;
}
if (typeof ref.has === 'function') {
return ref.has(key);
}

return (justFirst ? null : common);
return ref[key] !== undefined;
};


Expand Down Expand Up @@ -751,50 +716,6 @@ exports.once = function (method) {
exports.ignore = function () { };


exports.transform = function (source, transform, options) {

exports.assert(source === null || source === undefined || typeof source === 'object' || Array.isArray(source), 'Invalid source object: must be null, undefined, an object, or an array');
const separator = (typeof options === 'object' && options !== null) ? (options.separator || '.') : '.';

if (Array.isArray(source)) {
const results = [];
for (let i = 0; i < source.length; ++i) {
results.push(exports.transform(source[i], transform, options));
}

return results;
}

const result = {};
const keys = Object.keys(transform);

for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
const path = key.split(separator);
const sourcePath = transform[key];

exports.assert(typeof sourcePath === 'string', 'All mappings must be "." delineated strings');

let segment;
let res = result;

while (path.length > 1) {
segment = path.shift();
if (!res[segment]) {
res[segment] = {};
}

res = res[segment];
}

segment = path.shift();
res[segment] = exports.reach(source, sourcePath, options);
}

return result;
};


exports.uniqueFilename = function (path, extension) {

if (extension) {
Expand All @@ -821,12 +742,6 @@ exports.stringify = function (...args) {
};


exports.shallow = function (source) {

return Object.assign({}, source);
};


exports.wait = function (timeout) {

return new Promise((resolve) => setTimeout(resolve, timeout));
Expand Down
Loading

0 comments on commit c1815d3

Please sign in to comment.