Skip to content

Commit

Permalink
Merge pull request #16 from adrienv1520/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
adrienv1520 authored Aug 11, 2020
2 parents d03dfa3 + 1a06888 commit 8a6c326
Show file tree
Hide file tree
Showing 10 changed files with 679 additions and 599 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## 1.2.0 - delivery @11/08/2020

- add math.minmax helper
- add object.compare helper
- fix requester
- add tests
- update readme

## 1.1.1 - delivery @18/06/2020

- update project description
Expand Down
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
- [Encodings](#encodings)
- [Image](#image)
- [isPng(buffer)](#ispngbuffer)
- [Math](#math)
- [minmax(array)](#minmaxarray)
- [Object](#object)
- [exists(thing)](#existsthing)
- [is(Type, thing)](#istype-thing)
Expand All @@ -38,6 +40,7 @@
- [isEmptyOwn(thing)](#isemptyownthing)
- [getType(thing)](#gettypething)
- [getTypeName(thing)](#gettypenamething)
- [compare(a, b)](#comparea-b)
- [clone(thing)](#clonething)
- [Requester](#requester)
- [requester(options): AsyncFunction](#requesteroptions-asyncfunction)
Expand Down Expand Up @@ -323,6 +326,27 @@ Whether a buffer is of png format.
- `buffer`**<Buffer\>**
- Returns: **<Boolean\>**

## Math

Math helper.

- `math`**<Object\>** with the following property:

### minmax(array)
Directly get min and max values in a specific array. Works with numbers or strings.
- `array`**<Array\>**<Number|String\>
- Returns: **<Object\>**
- min: **<Number|String\>**
- max: **<Number|String\>**

Examples:
```javascript
minmax([55, 9]); // { min: 9, max: 55 }
minmax(['a', 'b']); // { min: 'a', max: 'b' }
minmax({}); // {}
minmax(undefined|null|NaN); // {}
```

## Object
Object helper.

Expand Down Expand Up @@ -469,6 +493,26 @@ getTypeName(new MyError); // 'MyError'
getTypeName(undefined|null|NaN); // undefined
```

### compare(a, b)
Compare two numbers or two strings (ignoring case and accents).
- `a`**<String|Number\>**
- `b`**<String|Number\>**
- Returns: **<Object\>**
- inferior: **<Boolean\>**
- superior: **<Boolean\>**
- equal: **<Boolean\>**

Examples:
```javascript
compare('a', 'z'); // { inferior: true, superior: false, equal: false }
compare(55, 9); // { inferior: false, superior: true, equal: false }
compare(5, 5); // { inferior: false, superior: false, equal: true }
compare(null); // {}
compare(undefined); // {}
compare(NaN, NaN); // {}
compare(); // {}
```

### clone(thing)
Make a deep copy of a specified object. **Does not handle circular references, use with caution**.
- `thing`**<Any\>**
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
const cast = require('./cast');
const encodings = require('./encodings');
const image = require('./image');
const math = require('./math');
const object = require('./object');
const requester = require('./requester');
const string = require('./string');
Expand All @@ -19,6 +20,7 @@ module.exports = Object.freeze({
cast,
encodings,
image,
math,
object,
requester,
string,
Expand Down
45 changes: 45 additions & 0 deletions lib/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Number helper.
*
* - minmax(array) -> Object
*/
const { is, compare } = require('./object');

/**
* @func minmax
*
* directly get min and max values in a specific array, works with numbers and strings
* NOTE: for performance use.
*
* @param {Array} array
* @return {Object} { min, max }
*/
const minmax = function minmax(array) {
if (!is(Array, array)) {
return { min: undefined, max: undefined };
}

let min;
let max;

array.forEach((value) => {
if (max === undefined) {
max = value;
} else if (compare(value, max).superior) {
max = value;
}

if (min === undefined) {
min = value;
} else if (compare(value, min).inferior) {
min = value;
}
});

return { min, max };
};

// exports
module.exports = Object.freeze({
minmax,
});
31 changes: 31 additions & 0 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* - isEmptyOwn(thing) -> Boolean
* - getType(thing) -> constructor Object
* - getTypeName(thing) -> String
* - compare(a, b) -> Object
* - clone(thing) -> Object cloned
*/

Expand Down Expand Up @@ -131,6 +132,35 @@ const getTypeName = function getTypeName(thing) {
return type !== undefined ? type.name : undefined;
};

/**
* @func compare
*
* compare two numbers or two strings (ignoring case and accents)
* @param {Number|String} a
* @param {Number|String} b
* @return {Object} { inferior, superior, equal }
*/
const compare = function compare(a, b) {
if (is(Number, a) && is(Number, b)) {
return {
inferior: a < b,
superior: a > b,
equal: a === b,
};
}

if (is(String, a) && is(String, b)) {
const result = a.toLowerCase().localeCompare(b.toLowerCase());
return {
inferior: result < 0,
superior: result > 0,
equal: result === 0,
};
}

return {};
};

/**
* clone
*/
Expand Down Expand Up @@ -329,5 +359,6 @@ module.exports = Object.freeze({
isEmptyOwn,
getType,
getTypeName,
compare,
clone,
});
2 changes: 1 addition & 1 deletion lib/requester.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ const requester = function requester(options) {

resolve({ statusCode, headers, body: resBody });
} catch (e) {
const error = new Error(`unable to format response, ${e.message}`);
const error = new Error(`unable to format response in ${format}: ${finalBuffer.toString('utf8')}`);
error.name = 'RequesterError';
error.code = 'RESPONSE_FORMAT_ERROR';
return reject(error);
Expand Down
Loading

0 comments on commit 8a6c326

Please sign in to comment.