Browse Source

first attempt to replace underscore with lodash

Richard Icke 8 years ago
parent
commit
343e5777d1

+ 2 - 1
config.js

@@ -43,9 +43,10 @@ require.config({
         "text":                     "node_modules/text/text",
         "tpl":                      "node_modules/requirejs-undertemplate/tpl",
         "typeahead":                "components/typeahead.js/index",
+        "lodash":                   "node_modules/lodash/lodash",
         "underscore":               "node_modules/underscore/underscore",
         "utils":                    "src/utils",
-        
+
         // Converse
         "converse-api":             "src/converse-api",
         "converse-bookmarks":       "src/converse-bookmarks",

+ 1 - 1
docs/source/developer_guidelines.rst

@@ -112,7 +112,7 @@ Converse.js relies on the following dependencies:
 * `moment.js <http://momentjs.com/>`_ provides a better API for handling dates and times.
 * `Strophe.js <http://strophe.im/>`_ maintains the XMPP session, is used to
   build XMPP stanzas, to send them, and to register handlers for received stanzas.
-* `Underscore <http://underscorejs.org/>`_ provides very useful utility functions.
+* `lodash <https://lodash.com/>`_ provides very useful utility functions.
 * `Backbone <http://backbonejs.org/>`_ is used to model the data as Models and
   Collections and to create Views that render the UI.
 * `backbone.overview <http://github.com/jcbrand/backbone.overview>`_ provides

+ 1 - 1
docs/source/troubleshooting.rst

@@ -58,7 +58,7 @@ Solution:
 
 First, find out which object is referred to by ``Object [object Object]``.
 
-It will probably be the jQuery object ``$`` or perhaps the underscore.js object ``_``.
+It will probably be the jQuery object ``$`` or perhaps the lodash/underscore object ``_``.
 
 For the purpose of demonstration, I'm going to assume its ``$``, but the same
 rules apply if its something else.

+ 1 - 1
non_amd.html

@@ -46,7 +46,7 @@
     <!-- END STROPHE -->
 
     <!-- BEGIN BACKBONE -->
-    <script type="text/javascript" src="node_modules/underscore/underscore.js"></script>
+    <script type="text/javascript" src="node_modules/lodash/core.js"></script>
     <script type="text/javascript" src="node_modules/backbone//backbone.js"></script>
     <script type="text/javascript" src="node_modules/backbone.browserStorage/backbone.browserStorage.js"></script>
     <script type="text/javascript" src="node_modules/backbone.overview/backbone.overview.js"></script>

+ 4 - 3
package.json

@@ -49,6 +49,8 @@
     "jquery-easing": "0.0.1",
     "jquery.browser": ">=0.1.0",
     "jshint": "^2.9.4",
+    "lodash": "^4.17.4",
+    "lodash-cli": "^4.17.4",
     "moment": "~2.13.0",
     "otr": "0.2.16",
     "phantom-jasmine": "0.1.8",
@@ -57,11 +59,10 @@
     "po2json": "^0.4.4",
     "requirejs": "2.3.2",
     "requirejs-undertemplate": "~0.0.4",
-    "strophe.js": "1.2.12",
     "snyk": "^1.21.2",
+    "strophe.js": "1.2.12",
     "strophejs-plugins": "0.0.7",
-    "text": "requirejs/text#2.0.15",
-    "underscore": "~1.8.3"
+    "text": "requirejs/text#2.0.15"
   },
   "dependencies": {}
 }

+ 3 - 3
spec/bookmarks.js

@@ -2,15 +2,15 @@
     define([
         "jquery",
         "converse-api",
-        "underscore",
         "utils",
         "mock",
         "test_utils"
         ], factory);
-} (this, function ($, converse_api, _, utils, mock, test_utils) {
+} (this, function ($, converse_api, utils, mock, test_utils) {
     "use strict";
     var $iq = converse_api.env.$iq,
-        Strophe = converse_api.env.Strophe;
+        Strophe = converse_api.env.Strophe,
+        _ = converse_api.env._;
 
     describe("A chat room", function () {
 

+ 2 - 2
spec/converse.js

@@ -2,11 +2,11 @@
     define([
         "jquery",
         "converse-api",
-        "underscore",
         "mock",
         "test_utils"], factory);
-} (this, function ($, converse_api, _, mock, test_utils) {
+} (this, function ($, converse_api, mock, test_utils) {
     var b64_sha1 = converse_api.env.b64_sha1;
+    var _ = converse_api.env._;
 
     describe("Converse", function() {
         

+ 2 - 2
spec/transcripts.js

@@ -2,14 +2,14 @@
 (function (root, factory) {
     define([
         "jquery",
-        "underscore",
         "mock",
         "test_utils",
         "utils",
         "transcripts"
         ], factory
     );
-} (this, function ($, _, mock, test_utils, utils, transcripts) {
+} (this, function ($, mock, test_utils, utils, transcripts) {
+    var _ = converse_api.env._;
     var Strophe = converse_api.env.Strophe;
     var IGNORED_TAGS = [
         'stream:features',

+ 1 - 1
src/build-no-dependencies.js

@@ -36,7 +36,7 @@
         "strophe.ping",
         "typeahead",
         "otr",
-        "underscore"
+        "lodash"
     ],
     wrap: {
         endFile: ["wrapper-no-jquery.js", "wrapper-no-deps.js"]

+ 3 - 3
src/converse-api.js

@@ -8,7 +8,7 @@
 (function (root, factory) {
     define("converse-api", [
             "jquery",
-            "underscore",
+            "lodash",
             "moment_with_locales",
             "strophe",
             "utils",
@@ -75,10 +75,10 @@
             'set': function (key, val) {
                 var o = {};
                 if (typeof key === "object") {
-                    _.extend(converse, _.pick(key, Object.keys(converse.default_settings)));
+                    _.assignIn(converse, _.pick(key, Object.keys(converse.default_settings)));
                 } else if (typeof key === "string") {
                     o[key] = val;
-                    _.extend(converse, _.pick(o, Object.keys(converse.default_settings)));
+                    _.assignIn(converse, _.pick(o, Object.keys(converse.default_settings)));
                 }
             }
         },

+ 5 - 5
src/converse-bookmarks.js

@@ -12,7 +12,7 @@
 (function (root, factory) {
     define("converse-bookmarks", [
             "jquery",
-            "underscore",
+            "lodash",
             "moment_with_locales",
             "strophe",
             "utils",
@@ -78,7 +78,7 @@
                         var div = document.createElement('div');
                         div.innerHTML = html;
                         var bookmark_button = converse.templates.chatroom_bookmark_toggle(
-                            _.extend(
+                            _.assignIn(
                                 this.model.toJSON(),
                                 {
                                     info_toggle_bookmark: __('Bookmark this room'),
@@ -173,7 +173,7 @@
                     if (!models.length) {
                         this.renderBookmarkForm();
                     } else {
-                        _.each(models, function (model) {
+                        _.forEach(models, function (model) {
                             model.destroy();
                         });
                         this.$('.icon-pushpin').removeClass('button-on');
@@ -324,7 +324,7 @@
                         'items[node="storage:bookmarks"] item[id="current"] storage conference'
                     );
                     var that = this;
-                    _.each(bookmarks, function (bookmark) {
+                    _.forEach(bookmarks, function (bookmark) {
                         that.create({
                             'jid': bookmark.getAttribute('jid'),
                             'name': bookmark.getAttribute('name'),
@@ -391,7 +391,7 @@
                     var name = $(ev.target).data('bookmarkName');
                     var jid = $(ev.target).data('roomJid');
                     if (confirm(__(___("Are you sure you want to remove the bookmark \"%1$s\"?"), name))) {
-                        _.each(converse.bookmarks.where({'jid': jid}), function (item) { item.destroy(); });
+                        _.forEach(converse.bookmarks.where({'jid': jid}), function (item) { item.destroy(); });
                     }
                 },
 

+ 10 - 10
src/converse-core.js

@@ -9,7 +9,7 @@
 (function (root, factory) {
     define("converse-core", [
         "jquery",
-        "underscore",
+        "lodash",
         "polyfill",
         "utils",
         "moment_with_locales",
@@ -34,7 +34,7 @@
     Strophe = Strophe.Strophe;
 
     // Use Mustache style syntax for variable interpolation
-    /* Configuration of underscore templates (this config is distinct to the
+    /* Configuration of Lodash templates (this config is distinct to the
      * config of requirejs-tpl in main.js). This one is for normal inline templates.
      */
     _.templateSettings = {
@@ -64,7 +64,7 @@
         },
 
         on: function (evt, handler, context) {
-            if (_.contains(['ready', 'initialized'], evt)) {
+            if (_.includes(['ready', 'initialized'], evt)) {
                 converse.log('Warning: The "'+evt+'" event has been deprecated and will be removed, please use "connected".');
             }
             if (context) {
@@ -246,9 +246,9 @@
             xhr_custom_status: false,
             xhr_custom_status_url: '',
         };
-        _.extend(this, this.default_settings);
+        _.assignIn(this, this.default_settings);
         // Allow only whitelisted configuration attributes to be overwritten
-        _.extend(this, _.pick(settings, Object.keys(this.default_settings)));
+        _.assignIn(this, _.pick(settings, Object.keys(this.default_settings)));
 
         // BBB
         if (this.prebind === true) { this.authentication = converse.PREBIND; }
@@ -753,7 +753,7 @@
                 var bare_jid = Strophe.getBareJidFromJid(jid);
                 var resource = Strophe.getResourceFromJid(jid);
                 attributes.jid = bare_jid;
-                this.set(_.extend({
+                this.set(_.assignIn({
                     'id': bare_jid,
                     'jid': bare_jid,
                     'fullname': bare_jid,
@@ -985,7 +985,7 @@
                 name = _.isEmpty(name)? jid: name;
                 this.sendContactAddIQ(jid, name, groups,
                     function (iq) {
-                        var contact = this.create(_.extend({
+                        var contact = this.create(_.assignIn({
                             ask: undefined,
                             fullname: name,
                             groups: groups,
@@ -1246,7 +1246,7 @@
 
         this.RosterGroup = Backbone.Model.extend({
             initialize: function (attributes, options) {
-                this.set(_.extend({
+                this.set(_.assignIn({
                     description: DESC_GROUP_TOGGLE,
                     state: converse.OPENED
                 }, attributes));
@@ -1515,7 +1515,7 @@
                                     ' and allow_non_roster_messaging is set to false', 'error');
                         return;
                     }
-                    chatbox = this.create(_.extend({
+                    chatbox = this.create(_.assignIn({
                         'id': bare_jid,
                         'jid': bare_jid,
                         'fullname': jid,
@@ -1928,7 +1928,7 @@
             } else if (this.bosh_service_url) {
                 this.connection = new Strophe.Connection(
                     this.bosh_service_url,
-                    _.extend(this.connection_options, {'keepalive': this.keepalive})
+                    _.assignIn(this.connection_options, {'keepalive': this.keepalive})
                 );
             } else {
                 throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");

+ 3 - 3
src/utils.js

@@ -3,7 +3,7 @@
     define([
         "jquery",
         "jquery.browser",
-        "underscore",
+        "lodash",
         "tpl!field",
         "tpl!select_option",
         "tpl!form_select",
@@ -78,7 +78,7 @@
                     }
                 }
                 $obj.html(x);
-                _.each(list, function (url) {
+                _.forEach(list, function (url) {
                     isImage(url).then(function (ev) {
                         var prot = url.indexOf('http://') === 0 || url.indexOf('https://') === 0 ? '' : 'http://';
                         var escaped_url = encodeURI(decodeURI(url)).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
@@ -325,7 +325,7 @@
             return function (item) {
                 if (typeof attr === 'object') {
                     var value = false;
-                    _.each(attr, function (a) {
+                    _.forEach(attr, function (a) {
                         value = value || item.get(a).toLowerCase().indexOf(query.toLowerCase()) !== -1;
                     });
                     return value;

+ 1 - 1
src/wrapper-end.js

@@ -2,7 +2,7 @@
 define('jquery', [], function () { return jQuery; });
 define('jquery.browser', [], function () { return jQuery; });
 define('typeahead', [], function () { return jQuery; });
-define('underscore', [], function () { return _; });
+define('lodash', [], function () { return _; });
 define('moment_with_locales', [], function () { return moment; });
 define('strophe', [], function () {
     return {

+ 1 - 1
src/wrapper-no-deps.js

@@ -1,7 +1,7 @@
 /*global jQuery, _, moment, Strophe, $build, $iq, $msg, $pres, SHA1, Base64, MD5, DSA, OTR */
 define('jquery.browser', [], function () { return jQuery; });
 define('typeahead', [], function () { return jQuery; });
-define('underscore', [], function () { return _; });
+define('lodash', [], function () { return _; });
 define('moment_with_locales', [], function () { return moment; });
 define('strophe', [], function () {
     return {

+ 1 - 0
tests/mock.js

@@ -1,6 +1,7 @@
 (function (root, factory) {
     define("mock", ['converse'], factory);
 }(this, function (converse_api) {
+    var _ = converse_api.env._;
     var Strophe = converse_api.env.Strophe;
     var $iq = converse_api.env.$iq;
     var mock = {};