Skip to content

Commit

Permalink
feat: add TS declaration file
Browse files Browse the repository at this point in the history
Closes #243
  • Loading branch information
kenany committed Feb 26, 2024
1 parent 876e3de commit da04ce2
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ jobs:
- run: npm ls
- run: npm test
- run: npm run lint
- run: npm run type-coverage
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
coverage
node_modules
*.log
*.d.ts
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2013–2023 Kenan Yildirim <https://kenany.me/>
Copyright 2013–2024 Kenan Yildirim <https://kenany.me/>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ const isEmpty = require('lodash.isempty');
const isPlainObject = require('lodash.isplainobject');
const strftime = require('strftime');

/**
* @typedef {object} Options
* @property {number} [indent] Number of spaces for indentation.
* @property {boolean} [newlineAfterSection] Whether or not to output a newline
* after the last pair in a hash if that hash wasn't empty.
*/

/**
* @param {unknown} obj
* @returns {string}
Expand All @@ -16,7 +23,7 @@ function format(obj) {
}

/**
* @param {readonly unknown[][]} simplePairs
* @param {readonly [string, unknown][]} simplePairs
* @returns {boolean}
*/
function isArrayOfTables(simplePairs) {
Expand Down Expand Up @@ -55,7 +62,7 @@ function escapeKey(key) {

/**
* @param {object} hash
* @param {object} options
* @param {Options} options
* @returns {string}
*/
module.exports = function(hash, options = {}) {
Expand All @@ -65,11 +72,15 @@ module.exports = function(hash, options = {}) {
* @returns {void}
*/
function visit(hash, prefix) {
/** @type {[string, object][]} */
const nestedPairs = [];

/** @type {[string, unknown][]} */
const simplePairs = [];
const indentStr = ''.padStart(options.indent, ' ');
const indentStr = ''.padStart(options.indent || 0, ' ');

Object.keys(hash).forEach((key) => {
// @ts-expect-error
const value = hash[key];

if (value === undefined) {
Expand Down Expand Up @@ -106,6 +117,8 @@ module.exports = function(hash, options = {}) {
toml += '\n';
}
}

// @ts-expect-error Asserted to be an array at this point.
value.forEach((obj) => {
if (!isEmpty(prefix)) {
toml += '[[' + prefix + '.' + key + ']]\n';
Expand Down
22 changes: 20 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"license": "MIT",
"author": "Kenan Yildirim <[email protected]> (https://kenany.me/)",
"main": "index.js",
"types": "index.d.ts",
"files": [
"CHANGELOG.md",
"index.d.ts",
"index.js",
"LICENSE.txt"
],
Expand All @@ -21,9 +24,15 @@
"node": "18 || >=20"
},
"scripts": {
"clean": "rimraf --glob test/**/*.d.ts *.d.ts",
"lint": "eslint .",
"release": "semantic-release",
"test": "nyc --reporter html --reporter text tape test/*.js"
"type-coverage": "type-coverage --at-least 96 --detail --strict",
"prebuild": "npm run clean",
"build": "tsc",
"pretest": "npm run build",
"test": "nyc --reporter html --reporter text tape test/*.js",
"prepack": "npm run build"
},
"dependencies": {
"lodash.isdate": "^4.0.1",
Expand All @@ -35,10 +44,19 @@
"@kenan/eslint-config": "^11.1.3",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@tsconfig/node18": "^18.2.2",
"@types/lodash.isdate": "^4.0.9",
"@types/lodash.isempty": "^4.4.9",
"@types/lodash.isplainobject": "^4.0.9",
"@types/strftime": "^0.9.8",
"@types/tape": "^5.6.4",
"conventional-changelog-conventionalcommits": "^7.0.2",
"eslint": "^8.56.0",
"nyc": "^15.1.0",
"rimraf": "^5.0.5",
"semantic-release": "^23.0.2",
"tape": "^5.7.5"
"tape": "^5.7.5",
"type-coverage": "^2.27.1",
"typescript": "^5.3.3"
}
}
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@tsconfig/node18/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true
},
"include": ["index.js"]
}

0 comments on commit da04ce2

Please sign in to comment.