-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add flakey Date.prototype.getDay and Object.keys #3
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||||||||||||||||||||
import { generatePercentChanceToFlake, log, PERCENT_CHANCE_OF_SUCCESS } from './utils' | ||||||||||||||||||||||
import { clonedMathFloor, clonedMathRandom, generatePercentChanceToFlake, log, PERCENT_CHANCE_OF_SUCCESS } from './utils' | ||||||||||||||||||||||
const clonedNow = Date.now | ||||||||||||||||||||||
const clonedGetDay = Date.prototype.getDate | ||||||||||||||||||||||
|
||||||||||||||||||||||
Date.now = function () { | ||||||||||||||||||||||
if(generatePercentChanceToFlake() > PERCENT_CHANCE_OF_SUCCESS) { | ||||||||||||||||||||||
|
@@ -8,3 +9,19 @@ Date.now = function () { | |||||||||||||||||||||
} | ||||||||||||||||||||||
return clonedNow.apply(this, []) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
Date.prototype.getDay = function () { | ||||||||||||||||||||||
if (generatePercentChanceToFlake() > PERCENT_CHANCE_OF_SUCCESS) { | ||||||||||||||||||||||
log('Date.prototype.getDay flaking') | ||||||||||||||||||||||
let incorrectDay = clonedMathFloor(clonedMathRandom() * 7) | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Math.random() could theoretically return 1, which would make incorrectDay 7 | ||||||||||||||||||||||
// 7 is not a valid day of the week, so we need to change it to 6 | ||||||||||||||||||||||
if (incorrectDay === 7) { | ||||||||||||||||||||||
incorrectDay = 6 | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
return incorrectDay | ||||||||||||||||||||||
Comment on lines
+16
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
} | ||||||||||||||||||||||
return clonedGetDay.apply(this, []) | ||||||||||||||||||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { clonedMathFloor, clonedMathRandom, generatePercentChanceToFlake, log, PERCENT_CHANCE_OF_SUCCESS } from './utils'; | ||
|
||
const clonedKeys = Object.keys; | ||
|
||
Object.keys = function (obj: object): string[] { | ||
let keys = clonedKeys.call(this, obj); | ||
|
||
if (clonedMathRandom() < 0.5) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's shuffle based on generatePercentChanceToFlake. |
||
log('Object.keys shuffling'); | ||
keys = keys.sort(() => clonedMathRandom() - 0.5); | ||
} | ||
|
||
if (generatePercentChanceToFlake() > PERCENT_CHANCE_OF_SUCCESS) { | ||
log('Object.keys flaking'); | ||
|
||
if (keys.length > 0) { | ||
const randomIndex = clonedMathFloor(clonedMathRandom() * keys.length); | ||
keys.splice(randomIndex, 1); | ||
} | ||
|
||
if (clonedMathRandom() < 0.02) { | ||
keys.push('flaked'); | ||
} | ||
} | ||
|
||
return keys; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.