{"action":"created","issue":{"url":"https://api.github.com/repos/lodash/lodash/issues/2675","repository_url":"https://api.github.com/repos/lodash/lodash","labels_url":"https://api.github.com/repos/lodash/lodash/issues/2675/labels{/name}","comments_url":"https://api.github.com/repos/lodash/lodash/issues/2675/comments","events_url":"https://api.github.com/repos/lodash/lodash/issues/2675/events","html_url":"https://github.com/lodash/lodash/issues/2675","id":179090872,"number":2675,"title":"API proposal for _.replaceAll","user":{"login":"mk-pmb","id":3442602,"avatar_url":"https://avatars.githubusercontent.com/u/3442602?v=3","gravatar_id":"","url":"https://api.github.com/users/mk-pmb","html_url":"https://github.com/mk-pmb","followers_url":"https://api.github.com/users/mk-pmb/followers","following_url":"https://api.github.com/users/mk-pmb/following{/other_user}","gists_url":"https://api.github.com/users/mk-pmb/gists{/gist_id}","starred_url":"https://api.github.com/users/mk-pmb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mk-pmb/subscriptions","organizations_url":"https://api.github.com/users/mk-pmb/orgs","repos_url":"https://api.github.com/users/mk-pmb/repos","events_url":"https://api.github.com/users/mk-pmb/events{/privacy}","received_events_url":"https://api.github.com/users/mk-pmb/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/lodash/lodash/labels/duplicate","name":"duplicate","color":"cccccc"}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-09-25T15:08:35Z","updated_at":"2016-09-25T17:24:36Z","closed_at":"2016-09-25T17:24:36Z","body":"Splitting from #1084 in order to suggest a specific API.\r\n\r\nRe-considering my question about how a potential `replaceAll` method should handle search strings, I arrived at the decision that hiding a call to the RegExp constructor would be confusing, especially when there's no slot for RegExp flags. With that option discarded, I now recommmend this API:\r\n\r\n```\r\n_.replaceAll(what[, better[, text]])\r\n```\r\n\r\n  * All arguments are required, but not at the same time. If called with incomplete arguments, returns a partially pre-filled version of itself.\r\n  * Ensure `text` is a string, or convert it to string.\r\n  * If `what` looks RegExp-compatible, use native `text.replace`; otherwise, ensure it is a string and replace verbatim.\r\n\r\nExample implementation:\r\n\r\n```javascript\r\n_.replaceAll = function replaceAll(what, better, text) {\r\n  if (what === true) { what = this; }\r\n  if (arguments.length < 3) {\r\n    return Function.bind.apply(replaceAll,\r\n      Array.prototype.concat.apply([null], arguments));\r\n  }\r\n  return (what.exec ? String(text).replace(what, better)\r\n    : String(text).split(String(what)).join(better));\r\n};\r\n\r\nvar eq = function (v, e) { if (v !== e) { throw new Error(v); } },\r\n  checkAll = _.replaceAll('[ ]', '[x]'),\r\n  uncheck  = _.replaceAll(/(\\x5B|\\x28)[x•](\\x29|\\x5D)/g),\r\n  boxes =                     '[ ] yes   [ ] no   [x] really   (•) radio';\r\neq(checkAll(boxes),           '[x] yes   [x] no   [x] really   (•) radio');\r\neq(uncheck('$1 $2', boxes),   '[ ] yes   [ ] no   [ ] really   ( ) radio');\r\neq(uncheck('$1?$2', boxes),   '[ ] yes   [ ] no   [?] really   (?) radio');\r\neq(_.replaceAll('   ', ':', boxes), '[ ] yes:[ ] no:[x] really:(•) radio');\r\n```\r\n\r\nDepending on smartness of the VM's optimizer, it might be possible to increase performance by assuming a partially pre-filled function will be reused, do some checks and conversions upfront and return specialized functions."},"comment":{"url":"https://api.github.com/repos/lodash/lodash/issues/comments/249434167","html_url":"https://github.com/lodash/lodash/issues/2675#issuecomment-249434167","issue_url":"https://api.github.com/repos/lodash/lodash/issues/2675","id":249434167,"user":{"login":"jdalton","id":4303,"avatar_url":"https://avatars.githubusercontent.com/u/4303?v=3","gravatar_id":"","url":"https://api.github.com/users/jdalton","html_url":"https://github.com/jdalton","followers_url":"https://api.github.com/users/jdalton/followers","following_url":"https://api.github.com/users/jdalton/following{/other_user}","gists_url":"https://api.github.com/users/jdalton/gists{/gist_id}","starred_url":"https://api.github.com/users/jdalton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jdalton/subscriptions","organizations_url":"https://api.github.com/users/jdalton/orgs","repos_url":"https://api.github.com/users/jdalton/repos","events_url":"https://api.github.com/users/jdalton/events{/privacy}","received_events_url":"https://api.github.com/users/jdalton/received_events","type":"User","site_admin":false},"created_at":"2016-09-25T17:24:36Z","updated_at":"2016-09-25T17:24:36Z","body":"@mk-pmb Thanks for the walkthrough of your idea."}}
{"actor":{"display_login":"jdalton"}}