What are we trying to observe? Raw object data.
// Objects
var obj = { id: 2 };
obj.id = 3; // obj == { id: 3 }
// Arrays
var arr = ['foo', 'bar'];
arr.splice(1, 1, 'baz'); // arr == ['foo', 'baz'];
What are we trying to observe? Raw object data.
// Objects
var obj = { id: 2 };
obj.id = 3; // obj == { id: 3 }
// Arrays
var arr = ['foo', 'bar'];
arr.splice(1, 1, 'baz'); // arr == ['foo', 'baz'];
Computes a new version of a String value in which certain characters have been escaped, so that the regular expression engine will interpret any metacharacters that it may contain as character literals.
When the escape function is called with one argument string, the following steps are taken:
| // UTF8 | |
| var MeowUTF8 = (function(Meow_Root, undefined) { | |
| var meowHasUTF8; | |
| var m3, lR; | |
| var E, EE; | |
| var meowToHexStr; | |
| var meowEncodeURIcomponent; | |
| var Meow_UTFmd5; | |
| var Meow_Power = this; | |
| var meowUTF8ToBlocks; |
| //----- The ECMAScript 6 meta object protocol (MOP) implemented in ES5 | |
| // This is how getting a property is handled internally. | |
| // Double underscore (__) implies internal operation. | |
| Object.prototype.__Get__ = function (propKey, receiver) { | |
| receiver = receiver || this; | |
| var desc = this.__GetOwnProperty__(propKey); | |
| if (desc === undefined) { | |
| var parent = this.__GetPrototypeOf__(); | |
| if (parent === null) return undefined; |
| // Build a suffix array in O(n^2*log n); | |
| // kinda too much, but now you can search for a pattern in O(n*log n) where `n` is number of characters in `str` | |
| // | |
| function searchPattern(str) { | |
| var strLen = str.length; | |
| var suffixArray = new Array(strLen); | |
| var suffixes = new Array(strLen); | |
| for (var i = strLen-1; i >= 0; i--) { | |
| suffixes[i] = [i, str.substr(i)]; |
| /** | |
| * Applies HTML formatting to a raw string | |
| * | |
| * @param str {String} A raw string | |
| * @param formats {Array} An array of formats where each one has the following structure: [startOffset, endOffset, tagName] | |
| * @return formattedString {String} | |
| **/ | |
| function format(str, formats) { | |
| var formatsLen = formats.length; | |
| var strLen = str.length; |
| // For more info about indexed trees visit | |
| // http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees | |
| // | |
| // Sample run: | |
| // var tree = new IndexedTree([2, 5, 6, 9]) | |
| // tree.getSumIn(2, 3) -> 15 | |
| // | |
| // Why this is cool? It gives the sum of the values of a subarray in O(log n) time, | |
| // the update of a value also runs in O(log n) |
| // Construct a table with table[i] as the length of the longest prefix of the substring 0..i | |
| function longestPrefix(str) { | |
| // create a table of size equal to the length of `str` | |
| // table[i] will store the prefix of the longest prefix of the substring str[0..i] | |
| var table = new Array(str.length); | |
| var maxPrefix = 0; | |
| // the longest prefix of the substring str[0] has length | |
| table[0] = 0; |
| // Group all overlaping intervals | |
| // * * * * * * * | |
| // This is an approach to a problem the engineers at Google Calandar/ Outlook probably faced. | |
| // You have events that may overlap and you want to display them in such a way that | |
| // they don't overlap with each other. One approach is to distribute them into columns. | |
| // Each column has events that don't overlap with each other. | |
| // Cost: O(n*log n) if the interval aren't sorted by the starting time, | |
| // O(n) otherwise. | |
| // Sample run: groupOverlapingIntervals([ [2, 5], [5, 6],[3, 4] ]) |
| // Returns the closest point from a list to a given point | |
| // the naive approach runs in linear time, which is a quite slow if are calling this procedure a lot. | |
| // The following is an approach using a KD-tree. It runs in <O(n*log n), O(log n)> time | |
| // | |
| // Example: | |
| // var closetPoint = closetPointTree([[2, 3], [10, 17], [5, 6]]); | |
| // closetPoint([10, 16]) | |
| // -> [ 10, 17 ] | |
| function closetPointTree(points) { |