Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ for(let x = 0; x < 100; x++) {
// expectation: Today's date (1718201340130)
// enflaked result: Yesterday's date (1718114940130)
```
- Date.prototype.getDay(): returns an incorrect day of the week
```
const today = new Date();
console.log(today.getDay());
// expectation: Correct day of the week (0-6)
// enflaked result: Incorrect day of the week (0-6)
```
### Number
- Number.prototype.toString(): will return a string version of the incremented/decremented number
```
Expand Down Expand Up @@ -208,6 +215,17 @@ for(let x = 0; x < 100; x++) {
// expectation: 2
// enflaked result: 1
```
### Object
- Object.keys(): potentially changes the order of keys, removes a random key on flakiness, and occasionally adds a key "flaked".
```
const sampleObject = { a: 1, b: 2, c: 3 };
console.log(Object.keys(sampleObject));
// expectation: ["a", "b", "c"]
// enflaked results could be:
// ["b", "a", "c"] // keys shuffled
// ["a", "c"] // one key removed
// ["a", "b", "c", "flaked"] // "flaked" key added
```

## Contributing
Contributing is highly welcome- just make sure to follow the following:
Expand Down
19 changes: 18 additions & 1 deletion src/date.ts
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const clonedGetDay = Date.prototype.getDate
const clonedGetDay = Date.prototype.getDay


Date.now = function () {
if(generatePercentChanceToFlake() > PERCENT_CHANCE_OF_SUCCESS) {
Expand All @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
return Math.min(clonedMathFloor(clonedMathRandom() * 7), 6)

}
return clonedGetDay.apply(this, [])
};
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import './date'
import './json'
import './number'
import './string'
import './math'
import './math'
import './object'
27 changes: 27 additions & 0 deletions src/object.ts
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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's shuffle based on generatePercentChanceToFlake.
This is so when someone makes PERCENT_CHANCE_OF_SUCCESS 100, they can completely disable all possible weirdness

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;
};