diff --git a/README.md b/README.md index f700408..40a2c0b 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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: diff --git a/src/date.ts b/src/date.ts index 0dbd330..fa6862d 100644 --- a/src/date.ts +++ b/src/date.ts @@ -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 + } + return clonedGetDay.apply(this, []) +}; diff --git a/src/index.ts b/src/index.ts index 5826a53..b21fc44 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,4 +4,5 @@ import './date' import './json' import './number' import './string' -import './math' \ No newline at end of file +import './math' +import './object' \ No newline at end of file diff --git a/src/object.ts b/src/object.ts new file mode 100644 index 0000000..9084133 --- /dev/null +++ b/src/object.ts @@ -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) { + 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; +};