Skip to content

Commit

Permalink
feat: implement Result.fromJson
Browse files Browse the repository at this point in the history
  • Loading branch information
SPodjasek committed Sep 4, 2023
1 parent 6999745 commit 6e58049
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/dart_zxcvbn/lib/src/feedback.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class Feedback {
List<String>? suggestions,
}) : this.suggestions = suggestions ?? [];

factory Feedback.fromJson(Map<String, dynamic> json) => Feedback(
warning: json['warning'],
suggestions: List<String>.from(json['suggestions'], growable: false),
);

factory Feedback.getFeedback(
double score, List<EstimatedGuessesMixin> sequence) {
if (sequence.isEmpty) {
Expand Down
9 changes: 8 additions & 1 deletion packages/dart_zxcvbn/lib/src/matcher/common.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of '../zxcvbn.dart';

abstract interface class Match {
interface class Match {
/// The name of the matcher
final String pattern;

Expand All @@ -19,6 +19,13 @@ abstract interface class Match {
required this.j,
required this.token,
});

factory Match.fromJson(Map<String, dynamic> json) => Match(
pattern: json['pattern'],
i: json['i'],
j: json['j'],
token: json['token'],
);
}

abstract interface class Matcher {
Expand Down
16 changes: 16 additions & 0 deletions packages/dart_zxcvbn/lib/src/result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ class Result {
required this.calcTime,
});

factory Result.fromJson(Map<String, dynamic> json) => Result(
feedback: Feedback.fromJson(json['feedback']),
crackTimesSeconds:
CrackTimesSeconds.fromJson(json['crack_times_seconds']),
crackTimesDisplay:
CrackTimesDisplay.fromJson(json['crack_times_display']),
score: json['score'],
password: json['password'],
guesses: json['guesses'],
guessesLog10: json['guesses_log10'],
sequence: List<Match>.from(
json['sequence'].map((match) => Match.fromJson(match)),
growable: false),
calcTime: json['calc_time'],
);

@override
bool operator ==(Object other) =>
identical(this, other) ||
Expand Down
18 changes: 18 additions & 0 deletions packages/dart_zxcvbn/lib/src/time_estimates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ class CrackTimesSeconds {
required this.offlineFastHashing1e10PerSecond,
});

factory CrackTimesSeconds.fromJson(Map<String, dynamic> json) =>
CrackTimesSeconds(
onlineThrottling100PerHour: json['onlineThrottling100PerHour'],
onlineNoThrottling10PerSecond: json['onlineNoThrottling10PerSecond'],
offlineSlowHashing1e4PerSecond: json['offlineSlowHashing1e4PerSecond'],
offlineFastHashing1e10PerSecond:
json['offlineFastHashing1e10PerSecond'],
);

@override
bool operator ==(Object other) =>
identical(this, other) ||
Expand Down Expand Up @@ -55,6 +64,15 @@ class CrackTimesDisplay {
required this.offlineFastHashing1e10PerSecond,
});

factory CrackTimesDisplay.fromJson(Map<String, dynamic> json) =>
CrackTimesDisplay(
onlineThrottling100PerHour: json['onlineThrottling100PerHour'],
onlineNoThrottling10PerSecond: json['onlineNoThrottling10PerSecond'],
offlineSlowHashing1e4PerSecond: json['offlineSlowHashing1e4PerSecond'],
offlineFastHashing1e10PerSecond:
json['offlineFastHashing1e10PerSecond'],
);

@override
bool operator ==(Object other) =>
identical(this, other) ||
Expand Down

0 comments on commit 6e58049

Please sign in to comment.