Skip to content

Commit

Permalink
build: add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rkr00t committed Nov 29, 2023
1 parent c545704 commit 5897073
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 45 deletions.
21 changes: 13 additions & 8 deletions .eslintrc.json
Expand Up @@ -40,11 +40,14 @@
"no-sparse-arrays": [2],
"no-unreachable": [2],
"use-isnan": [2],
"valid-jsdoc": [2, {
"requireReturn": false,
"requireParamDescription": false,
"requireReturnDescription": false
}],
"valid-jsdoc": [
2,
{
"requireReturn": false,
"requireParamDescription": false,
"requireReturnDescription": false
}
],
"valid-typeof": [2],
"no-unexpected-multiline": [2],

Expand Down Expand Up @@ -125,7 +128,7 @@
// stylistic
"array-bracket-spacing": [2, "never"],
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
"camelcase": [2, {"properties": "never"}],
"camelcase": [2, { "properties": "never" }],
"comma-spacing": [2, { "before": false, "after": true }],
"comma-style": [2, "last"],
"computed-property-spacing": [2, "never"],
Expand Down Expand Up @@ -157,15 +160,17 @@
"object-curly-spacing": [2, "always"],
"one-var": [2, { "uninitialized": "always", "initialized": "never" }],
"operator-assignment": [0],
"operator-linebreak": [2, "before"],
"padded-blocks": [0],
"quote-props": [2, "as-needed"],
"quotes": [2, "single"],
"semi-spacing": [2, { "before": false, "after": true }],
"semi": [2, "always"],
"sort-vars": [0],
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
"space-before-function-paren": [
2,
{ "anonymous": "always", "named": "never" }
],
"space-in-parens": [2, "never"],
"space-infix-ops": [2],
"space-unary-ops": [2],
Expand Down
4 changes: 4 additions & 0 deletions .prettierrc
@@ -0,0 +1,4 @@
{
"trailingComma": "none",
"singleQuote": true
}
24 changes: 21 additions & 3 deletions lib/index.js
Expand Up @@ -10,7 +10,13 @@
* @return {String}
*/
function lessThanZeroError(p) {
return 'Expect percentile to be >= 0 but given "' + p + '" and its type is "' + (typeof p) + '".';
return (
'Expect percentile to be >= 0 but given "' +
p +
'" and its type is "' +
typeof p +
'".'
);
}

/**
Expand All @@ -21,7 +27,13 @@ function lessThanZeroError(p) {
* @return {String}
*/
function greaterThanHundredError(p) {
return 'Expect percentile to be <= 100 but given "' + p + '" and its type is "' + (typeof p) + '".';
return (
'Expect percentile to be <= 100 but given "' +
p +
'" and its type is "' +
typeof p +
'".'
);
}

/**
Expand All @@ -32,7 +44,13 @@ function greaterThanHundredError(p) {
* @return {String}
*/
function nanError(p) {
return 'Expect percentile to be a number but given "' + p + '" and its type is "' + (typeof p) + '".';
return (
'Expect percentile to be a number but given "' +
p +
'" and its type is "' +
typeof p +
'".'
);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -22,10 +22,12 @@
"coverage": "nyc --reporter=lcov --reporter=text --reporter=html npm test",
"build:types": "rm -rf ./lib/index.d.ts && tsc -p tsconfig.json",
"lint": "eslint lib test",
"format": "prettier ./lib/index.js ./test/index.js --write",
"format:check": "prettier ./lib/index.js ./test/index.js --check",
"bench": "node ./scripts/bench.js",
"test": "ava",
"ci:github-release": "conventional-github-releaser -p angular",
"pmm:prepare": "npm run build:types && npm run lint && npm test",
"pmm:prepare": "npm run format:check && npm run build:types && npm run lint && npm test",
"release:major": "pmm major",
"release:minor": "pmm minor",
"release:patch": "pmm patch"
Expand All @@ -38,7 +40,6 @@
"pre-commit": [
"pmm:prepare"
],
"dependencies": {},
"devDependencies": {
"ava": "^5.3.1",
"benchmark": "^2.1.4",
Expand All @@ -48,6 +49,7 @@
"nyc": "^15.1.0",
"pmm": "^2.0.0",
"pre-commit": "^1.2.2",
"prettier": "^3.1.0",
"typescript": "^5.3.2"
},
"config": {
Expand Down
93 changes: 61 additions & 32 deletions test/index.js
Expand Up @@ -22,84 +22,113 @@ const stubsSimple = [
{ percentile: 25, list: shuffleArray(generateArraySimple(100)), result: 25 },
{ percentile: 50, list: shuffleArray(generateArraySimple(100)), result: 50 },
{ percentile: 75, list: shuffleArray(generateArraySimple(100)), result: 75 },
{ percentile: 100, list: shuffleArray(generateArraySimple(100)), result: 100 },
{
percentile: 100,
list: shuffleArray(generateArraySimple(100)),
result: 100
},

{ percentile: 75, list: [NaN, NaN, 1, 100], result: 1 },
{ percentile: 75, list: [1, 100, NaN, NaN], result: 1 },
{ percentile: 75, list: shuffleArray([].concat(generateArraySimple(100), generateArraySimple(30))), result: 68 }
{
percentile: 75,
list: shuffleArray(
[].concat(generateArraySimple(100), generateArraySimple(30))
),
result: 68
}
];

test('percentile of simple values', t => {
stubsSimple.forEach(stub => {
t.is(
percentile(stub.percentile, stub.list),
stub.result
);
test('percentile of simple values', (t) => {
stubsSimple.forEach((stub) => {
t.is(percentile(stub.percentile, stub.list), stub.result);
});
});

test('percentile with negative values', t => {
test('percentile with negative values', (t) => {
t.is(percentile(50, shuffleArray([-1, -2, -3, -4, -5])), -3);
t.is(percentile(50, shuffleArray([7, 6, -1, -2, -3, -4, -5])), -2);
});

test('percentile of simple values in a typed array', t => {
stubsSimple.forEach(stub => {
t.is(
percentile(stub.percentile, Uint16Array.from(stub.list)),
stub.result
);
test('percentile of simple values in a typed array', (t) => {
stubsSimple.forEach((stub) => {
t.is(percentile(stub.percentile, Uint16Array.from(stub.list)), stub.result);
});
});

const stubsObject = [
{ percentile: 0, list: shuffleArray(generateArrayOfObject(100)), result: 1 },
{ percentile: 25, list: shuffleArray(generateArrayOfObject(100)), result: 25 },
{ percentile: 50, list: shuffleArray(generateArrayOfObject(100)), result: 50 },
{ percentile: 75, list: shuffleArray(generateArrayOfObject(100)), result: 75 },
{ percentile: 100, list: shuffleArray(generateArrayOfObject(100)), result: 100 },

{ percentile: 75, list: shuffleArray([].concat(generateArrayOfObject(100), generateArrayOfObject(30))), result: 68 }
{
percentile: 25,
list: shuffleArray(generateArrayOfObject(100)),
result: 25
},
{
percentile: 50,
list: shuffleArray(generateArrayOfObject(100)),
result: 50
},
{
percentile: 75,
list: shuffleArray(generateArrayOfObject(100)),
result: 75
},
{
percentile: 100,
list: shuffleArray(generateArrayOfObject(100)),
result: 100
},

{
percentile: 75,
list: shuffleArray(
[].concat(generateArrayOfObject(100), generateArrayOfObject(30))
),
result: 68
}
];

test('percentile of values in object', t => {
stubsObject.forEach(stub => {
test('percentile of values in object', (t) => {
stubsObject.forEach((stub) => {
t.is(
percentile(stub.percentile, stub.list, item => item.val).val,
percentile(stub.percentile, stub.list, (item) => item.val).val,
stub.result
);
});
});

test('array of percentiles', t => {
test('array of percentiles', (t) => {
t.deepEqual(
percentile([0, 25, 50, 75, 100], shuffleArray(generateArraySimple(100))),
[1, 25, 50, 75, 100]
);
});

test('array of percentiles when values are objects', t => {
test('array of percentiles when values are objects', (t) => {
t.deepEqual(
percentile([0, 25, 50, 75, 100], shuffleArray(generateArrayOfObject(100)), item => item.val).map(p => p.val),
percentile(
[0, 25, 50, 75, 100],
shuffleArray(generateArrayOfObject(100)),
(item) => item.val
).map((p) => p.val),
[1, 25, 50, 75, 100]
);
});


test('throw an error if NaN', t => {
test('throw an error if NaN', (t) => {
t.throws(() => {
percentile(undefined, []); // eslint-disable-line
});
});

test('throw an error if less than 0', t => {
test('throw an error if less than 0', (t) => {
t.throws(() => percentile(-1, []));
});

test('throw an error if grater than 100', t => {
test('throw an error if grater than 100', (t) => {
t.throws(() => percentile(101, []));
});

test('throws a list of errors', t => {
test('throws a list of errors', (t) => {
t.throws(() => percentile([101, -1, 'a'], []));
});

0 comments on commit 5897073

Please sign in to comment.