JavaScript count_chars
Returns info about what characters are used in input
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 | function count_chars (str, mode) { // Returns info about what characters are used in input // // version: 1008.1718 // discuss at: http://phpjs.org/functions/count_chars // + original by: Ates Goral (http://magnetiq.com) // + tweaked by: Jack // + bugfixed by: Onno Marsman // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + revised by: Theriault // * example 1: count_chars("Hello World!", 3); // * returns 1: "!HWdelor" // * example 2: count_chars("Hello World!", 1); // * returns 2: {32:1,33:1,72:1,87:1,100:1,101:1,108:3,111:2,114:1} var result = {}, resultArr = [], i; str = ('' + str).split('').sort().join('').match(/(.)\1*/g); if ((mode & 1) == 0) { for (i = 0; i != 256; i++) { result[i] = 0; } } if (mode === 2 || mode === 4) { for (i = 0; i != str.length; i += 1) { delete result[str[i].charCodeAt(0)]; } for (i in result) { result[i] = (mode === 4) ? String.fromCharCode(i) : 0; } } else if (mode === 3) { for (i = 0; i != str.length; i += 1) { result[i] = str[i].slice(0, 1); } } else { for (i = 0; i != str.length; i += 1) { result[str[i].charCodeAt(0)] = str[i].length; } } if (mode < 3) { return result; } for (i in result) { resultArr.push(result[i]); } return resultArr.join(''); } |
Examples
» Example 1
Running
1 | count_chars("Hello World!", 3); |
Should return
1 | "!HWdelor" |
» Example 2
Running
1 | count_chars("Hello World!", 1); |
Should return
1 | {32:1,33:1,72:1,87:1,100:1,101:1,108:3,111:2,114:1} |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have count_chars goodness in JavaScript.
@ Ates Goral: Wicked Ates! Unfortunately there is one thing at example 6:
[CODE="Javascript"]
// * example 6: count_chars("Hello World!", 4);
// * returns 6: "\x01\x02\x03\x04\x05\x06\x07\b \n\v\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGIJKLMNOPQRSTUVXYZ[\\]^_`abcfghijkmnpqstuvwxyz{|}~\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"
[/CODE]
It pretty much destroyed the tester-page because of all the messed up characters so for now I had to exclude it :( Other than that... awesome :)


Michael White
2 Mar '08
<br />
Opera: !HWdelor
<br />
Other browsers: Helo Wrd!
<br />
Strange bug. If I find a fix I'll post it.