diff --git a/lib/src/ensure.dart b/lib/src/ensure.dart index 83b7fa0..b589a38 100644 --- a/lib/src/ensure.dart +++ b/lib/src/ensure.dart @@ -6,27 +6,27 @@ bool ensureCase(dynamic raw, Case target) { if (raw is Iterable) { return raw.isEmpty || raw.every((element) => ensureCase(element, target)); } - if (raw is! String) { - return false; - } - switch (target) { - case Case.lower: - return raw.toLowerCase() == raw; - case Case.upper: - return raw.toUpperCase() == raw; - case Case.camel: - return raw.toCamelCase() == raw; - case Case.kebab: - return raw.toKebabCase() == raw; - case Case.pascal: - return raw.toPascalCase() == raw; - case Case.sentence: - return raw.toSentenceCase() == raw; - case Case.snake: - return raw.toSnakeCase() == raw; - case Case.capital: - return raw.toCapitalCase() == raw; + if (raw is String) { + switch (target) { + case Case.lower: + return raw.toLowerCase() == raw; + case Case.upper: + return raw.toUpperCase() == raw; + case Case.camel: + return raw.toCamelCase() == raw; + case Case.kebab: + return raw.toKebabCase() == raw; + case Case.pascal: + return raw.toPascalCase() == raw; + case Case.sentence: + return raw.toSentenceCase() == raw; + case Case.snake: + return raw.toSnakeCase() == raw; + case Case.capital: + return raw.toCapitalCase() == raw; + } } + return false; } bool ensureFullStop(String raw, String target) { @@ -38,9 +38,6 @@ bool ensureLeadingBlank(String raw) { } bool ensureMaxLength(dynamic raw, num maxLength) { - if (raw == null) { - return true; - } if (raw is String) { return raw.length <= maxLength; } @@ -51,13 +48,12 @@ bool ensureMaxLength(dynamic raw, num maxLength) { } bool ensureMaxLineLength(String raw, num maxLineLength) { - return raw.split('\n').every((line) => ensureMaxLength(line, maxLineLength)); + return raw + .split(RegExp(r'(?:\r?\n)')) + .every((line) => ensureMaxLength(line, maxLineLength)); } bool ensureMinLength(dynamic raw, num minLength) { - if (raw == null) { - return false; - } if (raw is String) { return raw.length >= minLength; } @@ -68,9 +64,6 @@ bool ensureMinLength(dynamic raw, num minLength) { } bool ensureEmpty(dynamic raw) { - if (raw == null) { - return true; - } if (raw is String) { return raw.isEmpty; } @@ -81,9 +74,6 @@ bool ensureEmpty(dynamic raw) { } bool ensureEnum(dynamic raw, Iterable enums) { - if (raw == null) { - return true; - } if (raw is String) { return raw.isEmpty || enums.contains(raw); } diff --git a/lib/src/rules.dart b/lib/src/rules.dart index 116b884..bc5f684 100644 --- a/lib/src/rules.dart +++ b/lib/src/rules.dart @@ -39,14 +39,14 @@ Map get supportedRules => { 'references-empty': emptyRule(CommitComponent.references), }; -/// Build full stop rule for commit component. +/// Build full-stop rule for commit component. RuleFunction fullStopRule(CommitComponent component) { return (Commit commit, Rule config) { if (config is! ValueRule) { throw Exception('$config is not ValueRuleConfig'); } final raw = commit.componentRaw(component); - final result = raw != null && ensureFullStop(raw, config.value); + final result = raw == null || ensureFullStop(raw, config.value); final negated = config.condition == RuleCondition.never; return RuleOutcome( valid: negated ? !result : result, @@ -59,11 +59,11 @@ RuleFunction fullStopRule(CommitComponent component) { }; } -/// Build leanding blank rule for commit component. +/// Build leanding-blank rule for commit component. RuleFunction leadingBlankRule(CommitComponent component) { return (Commit commit, Rule config) { final raw = commit.componentRaw(component); - final result = raw != null && ensureLeadingBlank(raw); + final result = raw == null || ensureLeadingBlank(raw); final negated = config.condition == RuleCondition.never; return RuleOutcome( valid: negated ? !result : result, @@ -76,11 +76,11 @@ RuleFunction leadingBlankRule(CommitComponent component) { }; } -/// Build leanding blank rule for commit component. +/// Build empty rule for commit component. RuleFunction emptyRule(CommitComponent component) { return (Commit commit, Rule config) { final raw = commit.componentRaw(component); - final result = ensureEmpty(raw); + final result = raw == null || ensureEmpty(raw); final negated = config.condition == RuleCondition.never; return RuleOutcome( valid: negated ? !result : result, @@ -97,7 +97,7 @@ RuleFunction caseRule(CommitComponent component) { throw Exception('$config is not CaseRuleConfig'); } final raw = commit.componentRaw(component); - final result = raw != null && ensureCase(raw, config.type); + final result = raw == null || ensureCase(raw, config.type); final negated = config.condition == RuleCondition.never; return RuleOutcome( valid: negated ? !result : result, @@ -110,14 +110,14 @@ RuleFunction caseRule(CommitComponent component) { }; } -/// Build max length rule for commit component. +/// Build max-length rule for commit component. RuleFunction maxLengthRule(CommitComponent component) { return (Commit commit, Rule config) { if (config is! LengthRule) { throw Exception('$config is not LengthRuleConfig'); } final raw = commit.componentRaw(component); - final result = raw != null && ensureMaxLength(raw, config.length); + final result = raw == null || ensureMaxLength(raw, config.length); final negated = config.condition == RuleCondition.never; return RuleOutcome( valid: negated ? !result : result, @@ -130,14 +130,14 @@ RuleFunction maxLengthRule(CommitComponent component) { }; } -/// Build max line length rule for commit component. +/// Build max-line-length rule for commit component. RuleFunction maxLineLengthRule(CommitComponent component) { return (Commit commit, Rule config) { if (config is! LengthRule) { throw Exception('$config is not LengthRuleConfig'); } final raw = commit.componentRaw(component); - final result = raw != null && ensureMaxLineLength(raw, config.length); + final result = raw == null || ensureMaxLineLength(raw, config.length); final negated = config.condition == RuleCondition.never; return RuleOutcome( valid: negated ? !result : result, @@ -150,14 +150,14 @@ RuleFunction maxLineLengthRule(CommitComponent component) { }; } -/// Build min length rule for commit component. +/// Build min-length rule for commit component. RuleFunction minLengthRule(CommitComponent component) { return (Commit commit, Rule config) { if (config is! LengthRule) { throw Exception('$config is not LengthRuleConfig'); } final raw = commit.componentRaw(component); - final result = raw != null && ensureMinLength(raw, config.length); + final result = raw == null || ensureMinLength(raw, config.length); final negated = config.condition == RuleCondition.never; return RuleOutcome( valid: negated ? !result : result, @@ -170,13 +170,14 @@ RuleFunction minLengthRule(CommitComponent component) { }; } +/// Build enum rule for commit component. RuleFunction enumRule(CommitComponent component) { return (Commit commit, Rule config) { if (config is! EnumRule) { throw Exception('$config is not EnumRuleConfig'); } final raw = commit.componentRaw(component); - final result = ensureEnum(raw, config.allowed); + final result = raw == null || ensureEnum(raw, config.allowed); final negated = config.condition == RuleCondition.never; return RuleOutcome( valid: negated ? !result : result,