Skip to content

Commit

Permalink
Add noImplicitAny TypeScript option
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Jan 2, 2024
1 parent 392c869 commit d88a1bf
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 25 deletions.
43 changes: 31 additions & 12 deletions humanize-duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1551,12 +1551,18 @@
return c % 10 === 1 && c % 100 !== 11;
}

/**
* `Object.assign` for legacy environments. Difficult to make type-check.
*
* @param {...any} destination
*/
function assign(destination) {
var source;
for (var i = 1; i < arguments.length; i++) {
source = arguments[i];
for (var prop in source) {
if (has(source, prop)) {
// @ts-ignore
destination[prop] = source[prop];
}
}
Expand All @@ -1572,6 +1578,12 @@
return Object.prototype.toString.call(arg) === "[object Array]";
};

/**
* @template T
* @param {T} obj
* @param {keyof T} key
* @returns {boolean}
*/
function has(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}
Expand Down Expand Up @@ -1650,6 +1662,7 @@
if (char === ".") {
formattedCount += decimal;
} else {
// @ts-ignore because `char` should always be 0-9 at this point.
formattedCount += digitReplacements[char];
}
}
Expand Down Expand Up @@ -1843,8 +1856,15 @@

/**
* Create a humanizer, which lets you change the default options.
*
* @param {Options} [passedOptions]
*/
function humanizer(passedOptions) {
/**
* @param {number} ms
* @param {Options} [humanizerOptions]
* @returns {string}
*/
var result = function humanizer(ms, humanizerOptions) {
// Make sure we have a positive number.
//
Expand Down Expand Up @@ -1889,19 +1909,18 @@
*
* This is a wrapper around the default humanizer.
*/
var humanizeDuration = humanizer({});

humanizeDuration.getSupportedLanguages = function getSupportedLanguages() {
var result = [];
for (var language in LANGUAGES) {
if (has(LANGUAGES, language) && language !== "gr") {
result.push(language);
var humanizeDuration = assign(humanizer({}), {
getSupportedLanguages: function getSupportedLanguages() {
var result = [];
for (var language in LANGUAGES) {
if (has(LANGUAGES, language) && language !== "gr") {
result.push(language);
}
}
}
return result;
};

humanizeDuration.humanizer = humanizer;
return result;
},
humanizer: humanizer
});

// @ts-ignore
if (typeof define === "function" && define.amd) {
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.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"test": "node --test"
},
"devDependencies": {
"@types/ms": "^0.7.34",
"@types/node": "^20.10.6",
"csv-parse": "^5.5.3",
"eslint": "^8.56.0",
Expand Down
9 changes: 8 additions & 1 deletion test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ const humanizeDuration = require("..");
const { test } = require("node:test");
const assert = require("node:assert/strict");

/**
* @typedef {import("../humanize-duration.js").Options} Options
*/

test("throws an error when passed a bad language in the function", () => {
/** @param {Options} options */
const humanizingWith = (options) => () => humanizeDuration(10000, options);

assert.throws(humanizingWith({ language: "EN" }), Error);
assert.throws(humanizingWith({ language: "bad language" }), Error);
assert.throws(humanizingWith({ language: "" }), Error);
assert.throws(humanizingWith({ language: null }), Error);
assert.throws(
humanizingWith({ language: "bad language", fallback: null }),
humanizingWith({ language: "bad language", fallbacks: [] }),
Error
);
});
Expand All @@ -21,6 +26,7 @@ test("throws an error when passed a bad language in a humanizer", () => {
const humanizer = humanizeDuration.humanizer({
language: "bad language",
});
/** @param {any} [options] */
const humanizing = (options) => () => humanizer(10000, options);

assert.throws(humanizing(), Error);
Expand All @@ -33,6 +39,7 @@ test("throws an error when passed a bad language in a humanizer", () => {
});

test("should throw if fallbacks configuration is invalid", () => {
/** @param {any} options */
const humanizingWith = (options) => () => humanizeDuration(10000, options);

assert.throws(humanizingWith({ language: "es", fallbacks: [] }), Error);
Expand Down
4 changes: 4 additions & 0 deletions test/languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const { promisify } = require("node:util");

const readdir = promisify(fs.readdir);

/**
* @param {string} language
* @returns {import("../humanize-duration.js").Options}
*/
const options = (language) => ({
language,
delimiter: "+",
Expand Down
20 changes: 8 additions & 12 deletions test/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ test("package.json has `bugs`", () => {
});

test("package.json and bower.json largely match", () => {
[
"name",
"version",
"keywords",
"license",
"main",
"description",
"repository",
"homepage",
].forEach((key) => {
assert.deepStrictEqual(pkg[key], bower[key]);
});
assert.strictEqual(pkg.name, bower.name);
assert.strictEqual(pkg.version, bower.version);
assert.deepStrictEqual(pkg.keywords, bower.keywords);
assert.strictEqual(pkg.license, bower.license);
assert.strictEqual(pkg.main, bower.main);
assert.strictEqual(pkg.description, bower.description);
assert.deepStrictEqual(pkg.repository, bower.repository);
assert.strictEqual(pkg.homepage, bower.homepage);

const pkgAuthors = [pkg.author].concat(pkg.contributors);
assert.deepStrictEqual(pkgAuthors, bower.authors);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"target": "ES2022",
"strictBindCallApply": true,
"strictFunctionTypes": true,
"noImplicitAny": true,
"noImplicitThis": true,
"useUnknownInCatchVariables": true
}
Expand Down

0 comments on commit d88a1bf

Please sign in to comment.