Created
November 17, 2010 19:06
-
-
Save sgonyea/703852 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| reduce = (mapped, arg) -> | |
| opts = eval(arg) | |
| return mapped unless opts["phrases"]?.length > 0 | |
| return mapped unless mapped?.length > 0 | |
| p_regex = {} | |
| p_rplce = {} | |
| fields = arg["fields"] if arg["fields"]?.length > 0 | |
| cntr = 0 | |
| for phrase in opts["phrases"] | |
| p_regex[phrase] = /(#{phrase})/i | |
| p_rplce[phrase] = "<span class='ph#{cntr++}'>\1</span>" | |
| if fields? | |
| for _field of fields | |
| for _phrase of opts["phrases"] | |
| mapped[_field]?.replace(p_regex[_phrase], p_rplce[_phrase]) | |
| else | |
| for key, val in mapped | |
| val?.replace(p_regex[_phrase], p_rplce[_phrase]) | |
| mapped |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function(_mapped, _arg) { | |
| var reduce; | |
| var __hasProp = Object.prototype.hasOwnProperty; | |
| reduce = function(mapped, arg) { | |
| var _field, _i, _j, _len, _phrase, _ref, _ref2, cntr, fields, key, opts, p_regex, p_rplce, phrase, val; | |
| opts = eval(arg); | |
| if (!((opts["phrases"] == null ? undefined : opts["phrases"].length) > 0)) { | |
| return mapped; | |
| } | |
| if (!(((typeof mapped === "undefined" || mapped === null) ? undefined : mapped.length) > 0)) { | |
| return mapped; | |
| } | |
| p_regex = {}; | |
| p_rplce = {}; | |
| if ((arg["fields"] == null ? undefined : arg["fields"].length) > 0) { | |
| fields = arg["fields"]; | |
| } | |
| cntr = 0; | |
| _ref = opts["phrases"]; | |
| for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
| phrase = _ref[_i]; | |
| p_regex[phrase] = (new RegExp("(" + (phrase) + ")", "i")); | |
| p_rplce[phrase] = ("<span class='ph" + (cntr++) + "'>\1</span>"); | |
| } | |
| if (typeof fields !== "undefined" && fields !== null) { | |
| _ref = fields; | |
| for (_field in _ref) { | |
| if (!__hasProp.call(_ref, _field)) continue; | |
| _i = _ref[_field]; | |
| _ref2 = opts["phrases"]; | |
| for (_phrase in _ref2) { | |
| if (!__hasProp.call(_ref2, _phrase)) continue; | |
| _j = _ref2[_phrase]; | |
| mapped[_field] == null ? undefined : mapped[_field].replace(p_regex[_phrase], p_rplce[_phrase]); | |
| } | |
| } | |
| } else { | |
| _ref = mapped; | |
| for (val = 0, _len = _ref.length; val < _len; val++) { | |
| key = _ref[val]; | |
| (typeof val === "undefined" || val === null) ? undefined : val.replace(p_regex[_phrase], p_rplce[_phrase]); | |
| } | |
| } | |
| return mapped; | |
| }; | |
| return reduce(_mapped, _arg); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment