Skip to content

Commit

Permalink
Add type linting
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Aug 10, 2019
1 parent 15381be commit d394cc8
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 10 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @ts-check
/// <reference types="node" />

'use strict';

const slugify = require('./lib/slug').slugify;
Expand Down
3 changes: 3 additions & 0 deletions lib/placeholders.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @ts-check
/// <reference types="node" />

'use strict';

const strftime = require('strftime');
Expand Down
9 changes: 6 additions & 3 deletions lib/slug.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @ts-check
/// <reference types="node" />

'use strict';

const XRegExp = require('xregexp');
Expand Down Expand Up @@ -40,12 +43,12 @@ const SLUGIFY_TRAILING_LEADING_HYPHEN = /^-|-$/g;
*
* @name slugify
* @param {string} str - the string to create a slug of
* @param {string} [mode=default] - how string is slugified. Can be set to "default", "pretty" or "raw"
* @param {boolean} [cased=false] – whether to keep the character casing or not
* @param {string} [mode] - how string is slugified. Can be set to "default", "pretty" or "raw"
* @param {boolean} [cased] - whether to keep the character casing or not
* @return {string} the slugified string
* @see {@link https://github.com/jekyll/jekyll/blob/9278eb8fcec85b17573c6658d7d67ef6ea6ffb92/lib/jekyll/utils.rb#L177|Mimicked Jekyll Code}
*/
const slugify = function (str, mode, cased) {
const slugify = function (str, mode = 'default', cased = false) {
if (!str) { return str; }

mode = mode || 'default';
Expand Down
13 changes: 7 additions & 6 deletions lib/url.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @ts-check
/// <reference types="node" />

'use strict';

const urlPlaceholders = require('./placeholders').urlPlaceholders;
Expand Down Expand Up @@ -64,18 +67,16 @@ const generateUrl = function (template, jekyllResource) {
* @param {string} options.template - The String used as template for URL generation,
* or example "/:path/:basename:output_ext", where
* a placeholder is prefixed with a colon.
* @param {string} options.:placeholders - A hash containing the placeholders which will be
* @param {Object<string,any>} options.placeholders - A hash containing the placeholders which will be
* replaced when used inside the template. E.g.
* { year: (new Date()).getFullYear() } would replace
* the placeholder ":year" with the current year.
* @param {string} options.permalink - If supplied, no URL will be generated from the
* @param {string} [options.permalink] - If supplied, no URL will be generated from the
* template. Instead, the given permalink will be
* used as URL.
* @see {@link https://github.com/jekyll/jekyll/blob/cc82d442223bdaee36a2aceada64008a0106d82b/lib/jekyll/url.rb|Mimicked Jekyll Code}
*/
function JekyllUrl (options) {
options = options || {};

this.template = options.template;
this.placeholders = options.placeholders;
this.permalink = options.permalink;
Expand All @@ -92,7 +93,7 @@ function JekyllUrl (options) {
* @throws {Error} if the relative URL contains a colon
*/
JekyllUrl.prototype.toString = function () {
const sanitizedUrl = this.sanitize_url(this.generated_permalink() || this.generated_url());
const sanitizedUrl = this.sanitize_url(this.generated_permalink() || this.generated_url() || '');

if (sanitizedUrl.includes(':')) {
throw new Error('The URL' + sanitizedUrl + 'is invalid because it contains a colon.');
Expand Down Expand Up @@ -131,7 +132,7 @@ JekyllUrl.prototype.generated_url = function () {
*/
JekyllUrl.prototype.generate_url = function (template) {
return Object.keys(this.placeholders).reduce(
(result, token) => result.split(':' + token).join(this.constructor.escape_path(this.placeholders[token] || '')),
(result, token) => result.split(':' + token).join(JekyllUrl.escape_path(this.placeholders[token] || '')),
template
);
};
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"skip:check:docs": "documentation readme index.js -s \"API Usage\" --no-markdown-toc --diff-only",
"check:installed-check": "installed-check -i eslint",
"check:lint": "eslint .",
"check:types": "tsc",
"test:mocha": "NODE_ENV=test nyc --reporter=lcov --reporter text mocha test/**/*.spec.js",
"test": "run-p check:* && run-p test:*",
"build-docs": "documentation readme index.js -s \"API Usage\" --no-markdown-toc"
Expand All @@ -28,6 +29,9 @@
"node": ">=10.0.0"
},
"devDependencies": {
"@types/chai": "^4.2.0",
"@types/mocha": "^5.2.7",
"@types/node": "^12.7.1",
"chai": "^4.2.0",
"coveralls": "^3.0.5",
"dependency-check": "^4.1.0",
Expand All @@ -43,7 +47,8 @@
"installed-check": "^3.0.0",
"mocha": "^6.2.0",
"npm-run-all": "^4.1.5",
"nyc": "^14.1.1"
"nyc": "^14.1.1",
"typescript": "^3.5.3"
},
"dependencies": {
"strftime": "^0.10.0",
Expand Down
6 changes: 6 additions & 0 deletions test/slug.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// @ts-check
/// <reference types="node" />
/// <reference types="mocha" />
/// <reference types="chai" />

'use strict';

const chai = require('chai');
Expand All @@ -11,6 +16,7 @@ describe('Slug', function () {
// Tests taken from https://github.com/jekyll/jekyll/blob/70aa8a4e37cdbb935d8aacda3d6c6b598c2c91bb/test/test_utils.rb#L129

it('should return undefined if passed undefined', () => {
// @ts-ignore
should.not.exist(slugify());
});

Expand Down
6 changes: 6 additions & 0 deletions test/url.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// @ts-check
/// <reference types="node" />
/// <reference types="mocha" />
/// <reference types="chai" />

'use strict';

const chai = require('chai');
Expand All @@ -11,6 +16,7 @@ describe('Url', function () {
// Tests taken from https://github.com/jekyll/jekyll/blob/a29498eaaebbccd415cc3b811d050137f456cd9a/test/test_url.rb

it('should throw an exception if neither permalink or template is specified', () => {
// @ts-ignore
should.Throw(() => new JekyllUrl({ placeholders: {} }));
});

Expand Down
27 changes: 27 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

{
"files": [
"index.js"
],
"include": [
"lib/**/*.js",
"test/**/*.js"
],
"compilerOptions": {
/* Basic Options */
"target": "ES2016",
"module": "commonjs",
"allowJs": true,
"resolveJsonModule": true,
"noEmit": true,
"maxNodeModuleJsDepth": 3,

/* Strict Type-Checking Options */
"strict": true,
"noImplicitAny": false,
"strictNullChecks": false,

/* Additional Checks */
"noUnusedLocals": true
}
}

0 comments on commit d394cc8

Please sign in to comment.