{"action":"created","issue":{"url":"https://api.github.com/repos/parshap/node-sanitize-filename/issues/16","labels_url":"https://api.github.com/repos/parshap/node-sanitize-filename/issues/16/labels{/name}","comments_url":"https://api.github.com/repos/parshap/node-sanitize-filename/issues/16/comments","events_url":"https://api.github.com/repos/parshap/node-sanitize-filename/issues/16/events","html_url":"https://github.com/parshap/node-sanitize-filename/pull/16","id":115230349,"number":16,"title":"Implement the truncate function without node Buffer","user":{"login":"carltonf","id":1405740,"avatar_url":"https://avatars.githubusercontent.com/u/1405740?v=3","gravatar_id":"","url":"https://api.github.com/users/carltonf","html_url":"https://github.com/carltonf","followers_url":"https://api.github.com/users/carltonf/followers","following_url":"https://api.github.com/users/carltonf/following{/other_user}","gists_url":"https://api.github.com/users/carltonf/gists{/gist_id}","starred_url":"https://api.github.com/users/carltonf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carltonf/subscriptions","organizations_url":"https://api.github.com/users/carltonf/orgs","repos_url":"https://api.github.com/users/carltonf/repos","events_url":"https://api.github.com/users/carltonf/events{/privacy}","received_events_url":"https://api.github.com/users/carltonf/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-05T08:12:39Z","updated_at":"2015-11-06T06:59:03Z","closed_at":"2015-11-06T06:59:03Z","pull_request":{"url":"https://api.github.com/repos/parshap/node-sanitize-filename/pulls/16","html_url":"https://github.com/parshap/node-sanitize-filename/pull/16","diff_url":"https://github.com/parshap/node-sanitize-filename/pull/16.diff","patch_url":"https://github.com/parshap/node-sanitize-filename/pull/16.patch"},"body":"For easier and lightweight use in browser.\r\n\r\nI can run 'browserify index.js -s fnSanitize -o fn-sanitize.js' to get a version that works in a browser, but it takes in all Buffer-related code and amounts up to 51K uncompressed.\r\n\r\nSince \"utf8\" is the only concern here, it's possible to rewrite the truncate function without Buffer. The end result is a 3.5K uncompressed, much better for client side use.\r\n\r\nA slightly modified version is used in https://github.com/carltonf/bilibili-helper/commit/c7a80fe5ae2ab39cf9117a8244e8b22f0bdb0356 . It's used to sanitize downloaded file names which are auto-created from web page content.\r\n\r\nAll tests have passed and some tests for non-BMP cases are added."},"comment":{"url":"https://api.github.com/repos/parshap/node-sanitize-filename/issues/comments/154329878","html_url":"https://github.com/parshap/node-sanitize-filename/pull/16#issuecomment-154329878","issue_url":"https://api.github.com/repos/parshap/node-sanitize-filename/issues/16","id":154329878,"user":{"login":"carltonf","id":1405740,"avatar_url":"https://avatars.githubusercontent.com/u/1405740?v=3","gravatar_id":"","url":"https://api.github.com/users/carltonf","html_url":"https://github.com/carltonf","followers_url":"https://api.github.com/users/carltonf/followers","following_url":"https://api.github.com/users/carltonf/following{/other_user}","gists_url":"https://api.github.com/users/carltonf/gists{/gist_id}","starred_url":"https://api.github.com/users/carltonf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carltonf/subscriptions","organizations_url":"https://api.github.com/users/carltonf/orgs","repos_url":"https://api.github.com/users/carltonf/repos","events_url":"https://api.github.com/users/carltonf/events{/privacy}","received_events_url":"https://api.github.com/users/carltonf/received_events","type":"User","site_admin":false},"created_at":"2015-11-06T06:59:03Z","updated_at":"2015-11-06T06:59:03Z","body":"The implementation is more or less a mix from various stackoverflow answers and wikipedia entry on UTF-8.\r\nI think my main references are from:\r\nhttp://stackoverflow.com/questions/2219526/how-many-bytes-in-a-javascript-string\r\nhttp://stackoverflow.com/questions/5515869/string-length-in-bytes-in-javascript?lq=1\r\nhttps://en.wikipedia.org/wiki/UTF-8\r\nThe non-BMP char handling is referred from\r\nhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt#Fixing_charCodeAt()_to_handle_non-Basic-Multilingual-Plane_characters_if_their_presence_earlier_in_the_string_is_unknown\r\nThere can be others I can't remember.\r\n\r\nThe idea behind the implementation is quite straightforward actually, the specification states that different code points range use different number of bytes. The non-BMP chars being a little bit more complicated, as two JS chars are needed to represent one.\r\n\r\n https://github.com/DylanPiercey/byte-length/blob/master/index.js#L11-L16. is the same idea.\r\n\r\nAbout testing: I've included some edge cases test cases and assumed that the original test cases have normal cases covered. Any suggestions on other cases worth covering?"}}