diff --git a/src/LintIssue.js b/src/LintIssue.js deleted file mode 100755 index cbd83c36..00000000 --- a/src/LintIssue.js +++ /dev/null @@ -1,43 +0,0 @@ -const chalk = require('chalk'); -const logSymbols = require('log-symbols'); - -class LintIssue { - /** - * A lint issue. It could be an error or a warning. - * @typedef {Object} LintIssue - * @property {string} lintId Unique, lowercase, hyphen-separate name for the lint - * @property {string} severity 'error' or 'warning' - * @property {string} node Name of the node in the JSON the lint audits - * @property {string} lintMessage Human-friendly message to users - */ - - /** - * constructor - * @param {String} lintId Unique, lowercase, hyphen-separate name for the lint - * @param {String} severity 'error' or 'warning' - * @param {String} node Name of the node in the JSON the lint audits - * @param {String} lintMessage Human-friendly message to users - * @returns {LintIssue} An instance of {@link LintIssue}. - */ - constructor(lintId, severity, node, lintMessage) { - this.lintId = lintId; - this.severity = severity; - this.node = node; - this.lintMessage = lintMessage; - } - - /** - * Helper to convert the LintIssue to a printable string - * @returns {string} Human-friendly message about the lint issue - */ - toString() { - const logSymbol = this.severity === 'error' ? logSymbols.error : logSymbols.warning; - const formattedLintId = chalk.gray.dim(this.lintId); - const formattedNode = chalk.gray.bold(this.node); - const formattedMessage = this.severity === 'error' ? chalk.bold.red(this.lintMessage) : chalk.yellow(this.lintMessage); - - return `${logSymbol} ${formattedLintId} - node: ${formattedNode} - ${formattedMessage}`; - } -} - -module.exports = LintIssue; diff --git a/src/lint-issue.ts b/src/lint-issue.ts new file mode 100755 index 00000000..0dd70849 --- /dev/null +++ b/src/lint-issue.ts @@ -0,0 +1,66 @@ +import chalk from 'chalk'; +import logSymbols from 'log-symbols'; +import {Severity} from './types/severity'; + +/** + * A lint issue + */ +export class LintIssue { + /** + * Unique, lowercase, hyphen-separate name for the lint + * + * @type {string} + * @memberof LintIssue + */ + lintId: string; + /** + * 'error' or 'warning' + * + * @type {Severity} + * @memberof LintIssue + */ + severity: Severity; + /** + * Name of the node in the JSON the lint audits + * + * @type {string} + * @memberof LintIssue + */ + node: string + /** + * Human-friendly message to users + * + * @type {string} + * @memberof LintIssue + */ + lintMessage: string + + /** + * Creates an instance of LintIssue. + * @param lintId Unique, lowercase, hyphen-separate name for the lint + * @param severity 'error' or 'warning' + * @param node Name of the node in the JSON the lint audits + * @param lintMessage Human-friendly message to users + * @memberof LintIssue + */ + constructor(lintId: string, severity: Severity, node: string, lintMessage: string) { + this.lintId = lintId; + this.severity = severity; + this.node = node; + this.lintMessage = lintMessage; + } + + /** + * Helper to convert the LintIssue to a printable string + * + * @returns {string} Human-friendly message about the lint issue + */ + toString(): string { + const logSymbol = this.severity === Severity.Error ? logSymbols.error : logSymbols.warning; + const formattedLintId = chalk.gray.dim(this.lintId); + const formattedNode = chalk.gray.bold(this.node); + const formattedMessage = this.severity === Severity.Error ? chalk.bold.red(this.lintMessage) : chalk.yellow(this.lintMessage); + + return `${logSymbol} ${formattedLintId} - node: ${formattedNode} - ${formattedMessage}`; + } +} diff --git a/src/rules/bin-type.js b/src/rules/bin-type.js deleted file mode 100755 index bcb42ffe..00000000 --- a/src/rules/bin-type.js +++ /dev/null @@ -1,20 +0,0 @@ -const {isObject, isString} = require('../validators/type'); -const LintIssue = require('../LintIssue'); - -const lintId = 'bin-type'; -const nodeName = 'bin'; -const message = 'Type should be either a string or an Object'; -const ruleType = 'standard'; - -const lint = (packageJsonData, severity) => { - if (!isString(packageJsonData, nodeName) && !isObject(packageJsonData, nodeName)) { - return new LintIssue(lintId, severity, nodeName, message); - } - - return true; -}; - -module.exports = { - lint, - ruleType, -}; diff --git a/src/rules/bin-type.ts b/src/rules/bin-type.ts new file mode 100755 index 00000000..287589c9 --- /dev/null +++ b/src/rules/bin-type.ts @@ -0,0 +1,18 @@ +import {isObject, isString} from '../validators/type'; +import {LintIssue} from '../lint-issue'; +import {RuleType} from '../types/rule-type'; +import {Severity} from '../types/severity'; +import {PackageJson} from 'type-fest'; + +const lintId = 'bin-type'; +const nodeName = 'bin'; +const message = 'Type should be either a string or an Object'; +export const ruleType = RuleType.Standard; + +export const lint = (packageJsonData: PackageJson, severity: Severity): LintIssue | boolean => { + if (!isString(packageJsonData, nodeName) && !isObject(packageJsonData, nodeName)) { + return new LintIssue(lintId, severity, nodeName, message); + } + + return true; +}; diff --git a/src/types/lint-result.ts b/src/types/lint-result.ts new file mode 100644 index 00000000..f7fdfc90 --- /dev/null +++ b/src/types/lint-result.ts @@ -0,0 +1,3 @@ +export interface LintResult { + +} diff --git a/src/types/rule-type.ts b/src/types/rule-type.ts new file mode 100644 index 00000000..39c10e30 --- /dev/null +++ b/src/types/rule-type.ts @@ -0,0 +1,4 @@ +export enum RuleType { + Standard = 'standard', + +} diff --git a/src/types/severity.ts b/src/types/severity.ts new file mode 100644 index 00000000..32e04bf1 --- /dev/null +++ b/src/types/severity.ts @@ -0,0 +1,4 @@ +export enum Severity { + Error = 'error', + Warning = 'warning' +} diff --git a/src/validators/alphabetical-sort.js b/src/validators/alphabetical-sort.ts similarity index 57% rename from src/validators/alphabetical-sort.js rename to src/validators/alphabetical-sort.ts index a79138b8..726698e0 100755 --- a/src/validators/alphabetical-sort.js +++ b/src/validators/alphabetical-sort.ts @@ -1,12 +1,23 @@ +import {PackageJson} from 'type-fest'; + const increment = 1; +export interface IsInAlphabeticalOrderResult { + status: boolean; + data: { + invalidNode: string | null; + validNode: string | null; + } +} + /** * Determines whether an array is in alphabetical order - * @param {object} packageJsonData Valid JSON - * @param {string} nodeName Name of a node in the package.json file - * @return {object} Object containing the status and the dependencies that are out of order, if applicable + * + * @param packageJsonData Valid JSON + * @param nodeName Name of a node in the package.json file + * @return Object containing the status and the dependencies that are out of order, if applicable */ -const isInAlphabeticalOrder = (packageJsonData, nodeName) => { +export const isInAlphabeticalOrder = (packageJsonData: PackageJson, nodeName: string): IsInAlphabeticalOrderResult => { let isValid = true; let data = { invalidNode: null, @@ -31,7 +42,3 @@ const isInAlphabeticalOrder = (packageJsonData, nodeName) => { data, }; }; - -module.exports = { - isInAlphabeticalOrder, -}; diff --git a/src/validators/dependency-audit.js b/src/validators/dependency-audit.ts similarity index 84% rename from src/validators/dependency-audit.js rename to src/validators/dependency-audit.ts index c5d84d84..2a93c56b 100755 --- a/src/validators/dependency-audit.js +++ b/src/validators/dependency-audit.ts @@ -1,7 +1,7 @@ -/* eslint no-restricted-syntax: 'off', guard-for-in: 'off', no-continue: 'off' */ -const semver = require('semver'); +import semver from 'semver'; +import {PackageJson} from 'type-fest'; -const hasExceptions = (config) => typeof config === 'object' && config.hasOwnProperty('exceptions'); +const hasExceptions = (config: any): boolean => typeof config === 'object' && config.hasOwnProperty('exceptions'); /** * Determines whether or not the package has a given dependency @@ -10,7 +10,7 @@ const hasExceptions = (config) => typeof config === 'object' && config.hasOwnPro * @param {string} depsToCheckFor An array of packages to check for * @return {boolean} True if the package has a dependency. False if it is not or the node is missing. */ -const hasDependency = (packageJsonData, nodeName, depsToCheckFor) => { +export const hasDependency = (packageJsonData: PackageJson, nodeName: string, depsToCheckFor: string[]): boolean => { if (!packageJsonData.hasOwnProperty(nodeName)) { return false; } @@ -40,7 +40,7 @@ const hasDependency = (packageJsonData, nodeName, depsToCheckFor) => { * @param {string} depsToCheckFor An array of packages to check for * @return {boolean} True if the package has a pre-release version of a dependency. False if it is not or the node is missing. */ -const hasDepPrereleaseVers = (packageJsonData, nodeName, depsToCheckFor) => { +export const hasDepPrereleaseVers = (packageJsonData: PackageJson, nodeName: string, depsToCheckFor: string[]): boolean => { if (!packageJsonData.hasOwnProperty(nodeName)) { return false; } @@ -65,7 +65,7 @@ const hasDepPrereleaseVers = (packageJsonData, nodeName, depsToCheckFor) => { * @param {object} config Rule configuration * @return {boolean} True if the package has a dependency with version 0. False if it does not or the node is missing. */ -const hasDepVersZero = (packageJsonData, nodeName, config) => { +export const hasDepVersZero = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => { for (const dependencyName in packageJsonData[nodeName]) { if (hasExceptions(config) && config.exceptions.includes(dependencyName)) { continue; @@ -96,7 +96,7 @@ const hasDepVersZero = (packageJsonData, nodeName, config) => { * @param {String} rangeSpecifier A version range specifier * @return {Boolean} True if the version starts with the range, false if it doesn't. */ -const doesVersStartsWithRange = (dependencyVersion, rangeSpecifier) => { +export const doesVersStartsWithRange = (dependencyVersion: string, rangeSpecifier: string): boolean => { const firstCharOfStr = 0; return dependencyVersion.startsWith(rangeSpecifier, firstCharOfStr); @@ -110,7 +110,7 @@ const doesVersStartsWithRange = (dependencyVersion, rangeSpecifier) => { * @param {object} config Rule configuration * @return {boolean} False if the package has an invalid range. True if it is not or the node is missing. */ -const areVersRangesValid = (packageJsonData, nodeName, rangeSpecifier, config) => { +export const areVersRangesValid = (packageJsonData: PackageJson, nodeName: string, rangeSpecifier: string, config: any): boolean => { if (!packageJsonData.hasOwnProperty(nodeName)) { return true; } @@ -140,7 +140,7 @@ const areVersRangesValid = (packageJsonData, nodeName, rangeSpecifier, config) = * @param {object} config Rule configuration * @return {Boolean} True if any dependencies versions start with the invalid range, false if they don't. */ -const doVersContainInvalidRange = (packageJsonData, nodeName, rangeSpecifier, config) => { +export const doVersContainInvalidRange = (packageJsonData: PackageJson, nodeName: string, rangeSpecifier: string, config: any): boolean => { if (!packageJsonData.hasOwnProperty(nodeName)) { return false; } @@ -162,6 +162,11 @@ const doVersContainInvalidRange = (packageJsonData, nodeName, rangeSpecifier, co return containsInvalidVersion; }; +export interface AbsoluteVersionCheckerResult { + onlyAbsoluteVersionDetected: boolean; + dependenciesChecked: number; +} + /** * Determines whether or not all dependency versions are absolut * @param {object} packageJsonData Valid JSON @@ -169,7 +174,7 @@ const doVersContainInvalidRange = (packageJsonData, nodeName, rangeSpecifier, co * @param {object} config Rule configuration * @return {boolean} False if the package has an non-absolute version. True if it is not or the node is missing. */ -const absoluteVersionChecker = (packageJsonData, nodeName, config) => { +const absoluteVersionChecker = (packageJsonData: PackageJson, nodeName: string, config: any): AbsoluteVersionCheckerResult => { const notFound = -1; const firstCharOfStr = 0; let onlyAbsoluteVersionDetected = true; @@ -208,7 +213,7 @@ const absoluteVersionChecker = (packageJsonData, nodeName, config) => { * @param {object} config Rule configuration * @return {boolean} False if the package has an non-absolute version. True if it is not or the node is missing. */ -const areVersionsAbsolute = (packageJsonData, nodeName, config) => { +export const areVersionsAbsolute = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => { const {onlyAbsoluteVersionDetected, dependenciesChecked} = absoluteVersionChecker(packageJsonData, nodeName, config); return dependenciesChecked > 0 ? onlyAbsoluteVersionDetected : false; @@ -221,7 +226,7 @@ const areVersionsAbsolute = (packageJsonData, nodeName, config) => { * @param {object} config Rule configuration * @return {boolean} False if the package has an non-absolute version. True if it is not or the node is missing. */ -const doVersContainNonAbsolute = (packageJsonData, nodeName, config) => { +export const doVersContainNonAbsolute = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => { const {onlyAbsoluteVersionDetected, dependenciesChecked} = absoluteVersionChecker(packageJsonData, nodeName, config); return dependenciesChecked > 0 ? !onlyAbsoluteVersionDetected : false; @@ -234,21 +239,21 @@ const GITHUB_SHORTCUT_URL = /^(github:)?[^/]+\/[^/]+/; * @param version value of package's version * @return {boolean} True if the version is a shortcut to github repository */ -const isGithubRepositoryShortcut = (version) => GITHUB_SHORTCUT_URL.test(version); +const isGithubRepositoryShortcut = (version): boolean => GITHUB_SHORTCUT_URL.test(version); /** * Determines whether or not version is url to archive * @param version value of package's version * @return {boolean} True if the version is url to archive */ -const isArchiveUrl = (version) => version.endsWith('.tgz') || version.endsWith('.tar.gz') || version.endsWith('.zip'); +const isArchiveUrl = (version: string): boolean => version.endsWith('.tgz') || version.endsWith('.tar.gz') || version.endsWith('.zip'); /** * Determines whether or not version is git repository url * @param version value of package's version * @return {boolean} True if the version is an git repo url. */ -const isGitRepositoryUrl = (version) => { +const isGitRepositoryUrl = (version: string): boolean => { if (isArchiveUrl(version)) { return false; } @@ -275,7 +280,7 @@ const isGitRepositoryUrl = (version) => { * @param {object} config Rule configuration * @return {boolean} True if the package has an git repo. */ -const doVersContainGitRepository = (packageJsonData, nodeName, config) => { +export const doVersContainGitRepository = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => { for (const dependencyName in packageJsonData[nodeName]) { if (hasExceptions(config) && config.exceptions.includes(dependencyName)) { continue; @@ -298,7 +303,7 @@ const doVersContainGitRepository = (packageJsonData, nodeName, config) => { * @param {object} config Rule configuration * @return {boolean} True if the package contain archive url. */ -const doVersContainArchiveUrl = (packageJsonData, nodeName, config) => { +export const doVersContainArchiveUrl = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => { for (const dependencyName in packageJsonData[nodeName]) { if (hasExceptions(config) && config.exceptions.includes(dependencyName)) { continue; @@ -321,7 +326,7 @@ const doVersContainArchiveUrl = (packageJsonData, nodeName, config) => { * @param {object} config Rule configuration * @return {boolean} True if the package contain file url. */ -const doVersContainFileUrl = (packageJsonData, nodeName, config) => { +export const doVersContainFileUrl = (packageJsonData: PackageJson, nodeName: string, config: any): boolean => { for (const dependencyName in packageJsonData[nodeName]) { if (hasExceptions(config) && config.exceptions.includes(dependencyName)) { continue; @@ -336,17 +341,3 @@ const doVersContainFileUrl = (packageJsonData, nodeName, config) => { return false; }; - -module.exports = { - hasDependency, - hasDepPrereleaseVers, - hasDepVersZero, - doesVersStartsWithRange, - areVersRangesValid, - doVersContainInvalidRange, - areVersionsAbsolute, - doVersContainNonAbsolute, - doVersContainGitRepository, - doVersContainArchiveUrl, - doVersContainFileUrl, -}; diff --git a/src/validators/format.js b/src/validators/format.js deleted file mode 100755 index 908e8896..00000000 --- a/src/validators/format.js +++ /dev/null @@ -1,27 +0,0 @@ -const semver = require('semver'); - -/** - * Determines whether or not the string is lowercase - * @param {string} name Name - * @return {boolean} True if the string is lowercase or is missing. False if it is not. - */ -const isLowercase = (name) => name === name.toLowerCase(); - -/** - * Determines whether or not the node's value is a valid semantic version - * @param {object} packageJsonData Valid JSON - * @param {string} nodeName Name of a node in the package.json file - * @return {boolean} True if the node is a valid version number or is missing. False if it is not. - */ -const isValidVersionNumber = (packageJsonData, nodeName) => { - if (!packageJsonData.hasOwnProperty(nodeName)) { - return true; - } - - return semver.valid(packageJsonData[nodeName]) !== null; -}; - -module.exports = { - isLowercase, - isValidVersionNumber, -}; diff --git a/src/validators/format.ts b/src/validators/format.ts new file mode 100755 index 00000000..9a215a44 --- /dev/null +++ b/src/validators/format.ts @@ -0,0 +1,25 @@ +import semver from 'semver'; +import {PackageJson} from 'type-fest'; + +/** + * Determines whether or not the string is lowercase + * + * @param name Name + * @return True if the string is lowercase or is missing. False if it is not. + */ +export const isLowercase = (name: string): boolean => name === name.toLowerCase(); + +/** + * Determines whether or not the node's value is a valid semantic version + * + * @param packageJsonData Valid JSON + * @param nodeName Name of a node in the package.json file + * @return True if the node is a valid version number or is missing. False if it is not. + */ +export const isValidVersionNumber = (packageJsonData: PackageJson, nodeName: string): boolean => { + if (!packageJsonData.hasOwnProperty(nodeName)) { + return true; + } + + return semver.valid(packageJsonData[nodeName]) !== null; +}; diff --git a/src/validators/property-order.js b/src/validators/property-order.ts similarity index 81% rename from src/validators/property-order.js rename to src/validators/property-order.ts index 1a8bae26..c7d6f1e7 100755 --- a/src/validators/property-order.js +++ b/src/validators/property-order.ts @@ -1,4 +1,4 @@ -/* eslint max-statements: 'off' */ +import {PackageJson} from 'type-fest'; const notFound = -1; const empty = 0; @@ -52,13 +52,19 @@ const defaultPreferredNodeOrder = [ 'publishConfig', ]; +export interface IsInPreferredOrderResult { + status: boolean, + msg: string | null +} + /** * Determines whether an array is in the specified order - * @param {Object} packageJsonData Valid JSON - * @param {Array} userPreferredNodeOrder Preferred order of nodes - * @return {Object} Object containing the status and the node that is out of order, if applicable + * + * @param packageJsonData Valid JSON + * @param userPreferredNodeOrder Preferred order of nodes + * @return Object containing the status and the node that is out of order, if applicable */ -const isInPreferredOrder = (packageJsonData, userPreferredNodeOrder) => { +export const isInPreferredOrder = (packageJsonData: PackageJson, userPreferredNodeOrder: string[]): IsInPreferredOrderResult => { let isValid = true; let msg = null; const preferredNodeOrder = @@ -89,7 +95,3 @@ const isInPreferredOrder = (packageJsonData, userPreferredNodeOrder) => { msg, }; }; - -module.exports = { - isInPreferredOrder, -}; diff --git a/src/validators/property.js b/src/validators/property.ts similarity index 57% rename from src/validators/property.js rename to src/validators/property.ts index 4329e4b0..9bbe5be9 100755 --- a/src/validators/property.js +++ b/src/validators/property.ts @@ -1,19 +1,22 @@ -const parser = require('jsonc-parser'); +import parser from 'jsonc-parser'; +import {PackageJson} from 'type-fest'; /** * Determines whether or not the node exists in the package.json file - * @param {object} packageJsonData Valid JSON - * @param {string} nodeName Name of a node in the package.json file - * @return {boolean} True if the node exists. False if it is not. + * + * @param packageJsonData Valid JSON + * @param nodeName Name of a node in the package.json file + * @return True if the node exists. False if it is not. */ -const exists = (packageJsonData, nodeName) => packageJsonData.hasOwnProperty(nodeName); + export const exists = (packageJsonData: PackageJson, nodeName: string): boolean => packageJsonData.hasOwnProperty(nodeName); /** * Search for duplicate properties in package.json file - * @param {string} packageJsonSource JSON source string - * @return {string[]} List of duplicate property names. + * + * @param packageJsonSource JSON source string + * @return List of duplicate property names. */ -const findDuplicatePropNames = (packageJsonSource) => { +export const findDuplicatePropNames = (packageJsonSource: PackageJson): string[] => { const tree = parser.parseTree(packageJsonSource); if (!tree) { @@ -44,8 +47,3 @@ const findDuplicatePropNames = (packageJsonSource) => { return traverse(tree); }; - -module.exports = { - exists, - findDuplicatePropNames, -}; diff --git a/src/validators/type.js b/src/validators/type.js deleted file mode 100755 index 21dc49e2..00000000 --- a/src/validators/type.js +++ /dev/null @@ -1,62 +0,0 @@ -const isPlainObj = require('is-plain-obj'); - -/** - * Determines whether or not the node's value is an Array - * @param {object} packageJsonData Valid JSON - * @param {string} nodeName Name of a node in the package.json file - * @return {boolean} True if the node is an array or is missing. False if it is not. - */ -const isArray = (packageJsonData, nodeName) => { - if (!packageJsonData.hasOwnProperty(nodeName)) { - return true; - } - - return Array.isArray(packageJsonData[nodeName]); -}; - -/** - * Determines whether or not the node's value is a boolean - * @param {object} packageJsonData Valid JSON - * @param {string} nodeName Name of a node in the package.json file - * @return {boolean} True if the node is a boolean or is missing. False if it is not. - */ -const isBoolean = (packageJsonData, nodeName) => { - if (!packageJsonData.hasOwnProperty(nodeName)) { - return true; - } - - return typeof packageJsonData[nodeName] === 'boolean'; -}; - -/** - * Determines whether or not the node's value is an object - * @param {object} packageJsonData Valid JSON - * @param {string} nodeName Name of a node in the package.json file - * @return {boolean} True if the node is an object or is missing. False if it is not. - */ -const isObject = (packageJsonData, nodeName) => { - if (!packageJsonData.hasOwnProperty(nodeName)) { - return true; - } - - return isPlainObj(packageJsonData[nodeName]); -}; - -/** - * Determines whether or not the node's value is a string - * @param {object} packageJsonData Valid JSON - * @param {string} nodeName Name of a node in the package.json file - * @return {boolean} True if the node is a string or is missing. False if it is not. - */ -const isString = (packageJsonData, nodeName) => { - if (!packageJsonData.hasOwnProperty(nodeName)) { - return true; - } - - return typeof packageJsonData[nodeName] === 'string'; -}; - -module.exports.isArray = isArray; -module.exports.isBoolean = isBoolean; -module.exports.isObject = isObject; -module.exports.isString = isString; diff --git a/src/validators/type.ts b/src/validators/type.ts new file mode 100755 index 00000000..4b3ed744 --- /dev/null +++ b/src/validators/type.ts @@ -0,0 +1,62 @@ +import isPlainObj from 'is-plain-obj'; +import {PackageJson} from 'type-fest'; + +/** + * Determines whether or not the node's value is an Array + * + * @param packageJsonData Valid JSON + * @param nodeName Name of a node in the package.json file + * @return True if the node is an array or is missing. False if it is not. + */ +export const isArray = (packageJsonData: PackageJson, nodeName: string) => { + if (!packageJsonData.hasOwnProperty(nodeName)) { + return true; + } + + return Array.isArray(packageJsonData[nodeName]); +}; + +/** + * Determines whether or not the node's value is a boolean + * + * @param packageJsonData Valid JSON + * @param nodeName Name of a node in the package.json file + * @return True if the node is a boolean or is missing. False if it is not. + */ +export const isBoolean = (packageJsonData: PackageJson, nodeName: string) => { + if (!packageJsonData.hasOwnProperty(nodeName)) { + return true; + } + + return typeof packageJsonData[nodeName] === 'boolean'; +}; + +/** + * Determines whether or not the node's value is an object + * + * @param packageJsonData Valid JSON + * @param nodeName Name of a node in the package.json file + * @return True if the node is an object or is missing. False if it is not. + */ +export const isObject = (packageJsonData: PackageJson, nodeName: string) => { + if (!packageJsonData.hasOwnProperty(nodeName)) { + return true; + } + + return isPlainObj(packageJsonData[nodeName]); +}; + +/** + * Determines whether or not the node's value is a string + * + * @param packageJsonData Valid JSON + * @param nodeName Name of a node in the package.json file + * @return True if the node is a string or is missing. False if it is not. + */ +export const isString = (packageJsonData: PackageJson, nodeName: string) => { + if (!packageJsonData.hasOwnProperty(nodeName)) { + return true; + } + + return typeof packageJsonData[nodeName] === 'string'; +}; diff --git a/src/validators/valid-values.js b/src/validators/valid-values.js deleted file mode 100755 index 18b8fc3f..00000000 --- a/src/validators/valid-values.js +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Determines whether a node has a valid value - * @param {object} packageJsonData Valid JSON - * @param {string} nodeName Name of a node in the package.json file - * @param {String} value Value to validate - * @param {array} validValues Array of valid values to validate against - * @return {boolean} True if the node is equal to one of the valid values or is missing. False if it is not. - */ -const isValidValue = (packageJsonData, nodeName, value, validValues) => { - if (!packageJsonData.hasOwnProperty(nodeName)) { - return true; - } - - return validValues.includes(value); -}; - -/** - * Determines whether a node matches a valid value - * @param {object} packageJsonData Valid JSON - * @param {string} nodeName Name of a node in the package.json file - * @param {String} value Value to validate - * @param {array} validRegexes Array of regex to validate against - * @return {boolean} True if the node matches one of the valid regexes or is missing. False if it is not. - */ -const matchValidValue = (packageJsonData, nodeName, value, validRegexes) => { - if (!packageJsonData.hasOwnProperty(nodeName)) { - return true; - } - - return validRegexes.some((r) => r.test(value)); -}; - -module.exports.isValidValue = isValidValue; -module.exports.matchValidValue = matchValidValue; diff --git a/src/validators/valid-values.ts b/src/validators/valid-values.ts new file mode 100755 index 00000000..e3189efb --- /dev/null +++ b/src/validators/valid-values.ts @@ -0,0 +1,35 @@ +import {PackageJson} from 'type-fest'; + +/** + * Determines whether a node has a valid value + * + * @param packageJsonData Valid JSON + * @param nodeName Name of a node in the package.json file + * @param value Value to validate + * @param validValues Array of valid values to validate against + * @return True if the node is equal to one of the valid values or is missing. False if it is not. + */ +export const isValidValue = (packageJsonData: PackageJson, nodeName: string, value: any, validValues: any): boolean => { + if (!packageJsonData.hasOwnProperty(nodeName)) { + return true; + } + + return validValues.includes(value); +}; + +/** + * Determines whether a node matches a valid value + * + * @param packageJsonData Valid JSON + * @param nodeName Name of a node in the package.json file + * @param value Value to validate + * @param validRegexes Array of regex to validate against + * @return True if the node matches one of the valid regexes or is missing. False if it is not. + */ +export const matchValidValue = (packageJsonData: PackageJson, nodeName: string, value: any, validRegexes: any): boolean => { + if (!packageJsonData.hasOwnProperty(nodeName)) { + return true; + } + + return validRegexes.some((r) => r.test(value)); +};