Best JavaScript code snippet using root
grace-period-live-notification.service.spec.js
Source:grace-period-live-notification.service.spec.js  
...39    });40    describe('The registerListeners function', function() {41      it('should fail when not given a id', function() {42        expect(function() {43          gracePeriodLiveNotificationService.registerListeners();44        }).to.throw(Error);45        expect(gracePeriodLiveNotificationService.getListeners()).to.be.empty;46      });47      it('should save listeners', function() {48        gracePeriodLiveNotificationService.registerListeners('foo');49        expect(gracePeriodLiveNotificationService.getListeners().foo).to.exist;50      });51      it('should save listeners as many times as called', function() {52        gracePeriodLiveNotificationService.registerListeners('foo');53        gracePeriodLiveNotificationService.registerListeners('foo');54        gracePeriodLiveNotificationService.registerListeners('bar');55        expect(gracePeriodLiveNotificationService.getListeners().foo.length).to.equal(2);56        expect(gracePeriodLiveNotificationService.getListeners().bar.length).to.equal(1);57      });58    });59    describe('The unregisterListeners function', function() {60      it('should fail when task is null', function() {61        gracePeriodLiveNotificationService.registerListeners('foo');62        expect(gracePeriodLiveNotificationService.unregisterListeners).to.throw(Error);63        expect(gracePeriodLiveNotificationService.getListeners().foo.length).to.equal(1);64      });65      it('should remove listeners for given task', function() {66        gracePeriodLiveNotificationService.registerListeners('foo');67        gracePeriodLiveNotificationService.unregisterListeners('foo');68        expect(gracePeriodLiveNotificationService.getListeners().foo).to.not.exist;69      });70    });71  });72  describe('Events tests', function() {73    var gracePeriodLiveNotificationService, $rootScope;74    var liveNotificationMock;75    beforeEach(function() {76      var events = {};77      var handler = function(event, data) {78        if (!events[event]) {79          return;80        }81        events[event].forEach(function(listener) {82          listener(data);83        });84      };85      liveNotificationMock = function() {86        return {87          emit: handler,88          on: function(event, callback) {89            if (!events[event]) {90              events[event] = [];91            }92            events[event].push(callback);93          }94        };95      };96      module(function($provide) {97        $provide.value('livenotification', liveNotificationMock);98      });99      inject(function(_$rootScope_, _gracePeriodLiveNotificationService_) {100        $rootScope = _$rootScope_;101        gracePeriodLiveNotificationService = _gracePeriodLiveNotificationService_;102      });103    });104    describe('When started', function() {105      describe('on error event', function() {106        it('should make corresponding promise fail', function() {107          var sio = gracePeriodLiveNotificationService.start();108          var id = 'foo';109          var spyThatShouldBeCalled = sinon.spy();110          var spyThatShouldNotBeCalled = sinon.spy();111          gracePeriodLiveNotificationService.registerListeners(id).then(spyThatShouldNotBeCalled, spyThatShouldBeCalled);112          gracePeriodLiveNotificationService.registerListeners(id).then(spyThatShouldNotBeCalled);113          gracePeriodLiveNotificationService.registerListeners('bar', spyThatShouldNotBeCalled);114          $rootScope.$digest();115          sio.emit('graceperiod:error', {id: id});116          $rootScope.$digest();117          expect(spyThatShouldBeCalled).to.have.been.called;118          expect(spyThatShouldNotBeCalled).to.have.not.been.called;119        });120        it('should make corresponding promise fail only once', function() {121          var sio = gracePeriodLiveNotificationService.start();122          var id = 'foo';123          var errorSpy1 = sinon.spy();124          var errorSpy2 = sinon.spy();125          gracePeriodLiveNotificationService.registerListeners(id).catch(errorSpy1);126          gracePeriodLiveNotificationService.registerListeners(id).catch(errorSpy2);127          $rootScope.$digest();128          sio.emit('graceperiod:error', {id: id});129          $rootScope.$digest();130          sio.emit('graceperiod:error', {id: id});131          $rootScope.$digest();132          expect(errorSpy1).to.have.been.calledOnce;133          expect(errorSpy2).to.have.been.calledOnce;134        });135      });136      describe('on done event', function() {137        it('should run all the registered done listeners', function() {138          var sio = gracePeriodLiveNotificationService.start();139          var id = 'foo';140          var spyThatShouldBeCalled = sinon.spy();141          var spyThatShouldNotBeCalled = sinon.spy();142          gracePeriodLiveNotificationService.registerListeners(id).then(spyThatShouldBeCalled, spyThatShouldNotBeCalled);143          gracePeriodLiveNotificationService.registerListeners(id).catch(spyThatShouldNotBeCalled);144          gracePeriodLiveNotificationService.registerListeners('bar').catch(spyThatShouldNotBeCalled, spyThatShouldNotBeCalled);145          $rootScope.$digest();146          sio.emit('graceperiod:done', {id: id});147          $rootScope.$digest();148          expect(spyThatShouldBeCalled).to.have.been.called;149          expect(spyThatShouldNotBeCalled).to.have.not.been.called;150        });151        it('should run all the registered done listeners once', function() {152          var sio = gracePeriodLiveNotificationService.start();153          var id = 'foo';154          var spy1 = sinon.spy();155          var spy2 = sinon.spy();156          gracePeriodLiveNotificationService.registerListeners(id).then(spy1);157          gracePeriodLiveNotificationService.registerListeners(id).then(spy2);158          $rootScope.$digest();159          sio.emit('graceperiod:done', {id: id});160          $rootScope.$digest();161          sio.emit('graceperiod:done', {id: id});162          $rootScope.$digest();163          expect(spy1).to.have.been.calledOnce;164          expect(spy2).to.have.been.calledOnce;165        });166      });167    });168  });...focusblur.js
Source:focusblur.js  
...14function init()15{16   helpText = document.getElementById( "helpText" );17   18   registerListeners(document.getElementById( "form_name" ), 0 );19   registerListeners(document.getElementById( "form_nickname" ), 1 );20   registerListeners(document.getElementById( "form_email" ), 2 );21   registerListeners(document.getElementById( "form_desc" ), 3 );22   registerListeners(document.getElementById( "form_role_top" ), 4 );23   registerListeners(document.getElementById( "form_role_jg" ), 4 );24   registerListeners(document.getElementById( "form_role_mid" ), 4 );25   registerListeners(document.getElementById( "form_role_bot" ), 4 );26   registerListeners(document.getElementById( "form_role_supp" ), 4 );27   registerListeners(document.getElementById( "form_gender_male" ), 5 );28   registerListeners(document.getElementById( "form_gender_female" ), 5 );29   registerListeners(document.getElementById( "form_gender_other" ), 5 );30   registerListeners(document.getElementById( "form_rank" ), 6 );31   registerListeners(document.getElementById( "form_submit" ), 7 );32   registerListeners(document.getElementById( "form_clear" ), 8 );33}34function registerListeners( object, messageNumber )35{36   object.addEventListener( "focus", 37      function() { helpText.innerHTML = helpCommands[ messageNumber ]; },38      false );39   object.addEventListener( "blur", 40      function() { helpText.innerHTML = helpCommands[ 9 ]; }, false );41} ...Using AI Code Generation
1var root = require('./root.js');2root.registerListeners();3var root = require('./root.js');4root.registerListeners();5var test = require('./test.js');6test.registerListeners();7var test = require('./test.js');8test.registerListeners();9var root = require('./root.js');10root.registerListeners();11var root = require('./root.js');12root.registerListeners();13I am using node.js and I am trying to load a file from a local directory. I have tried using require, but it is not working. I have tried using fs and it is not working either. I am trying to load a file named 'test.txt'. I have tried the following code:var fs = require('fs');var text = fs.readFileSync('test.txt', 'utf8');console.log(text);The error I get is:Error: ENOENT, no such file or directory 'test.txt'Here is the directory structure:Here is the code I am using:14var fs = require('fs');15var csv = require('csv');16var csvFile = fs.createReadStream('test.csv');17var csvData = [];18csv()19.from(csvFile)20.to.array(function(data, count) {21    csvData = data;22});23console.log(csvData);Using AI Code Generation
1var root = require('./root');2root.registerListeners();3root.addEvent('appInit', function(){4    console.log('appInit event fired');5});6root.addEvent('appInit', function(){7    console.log('appInit event fired again');8});9root.addEvent('appInit', function(){10    console.log('appInit event fired again again');11});12root.addEvent('appInit', function(){13    console.log('appInit event fired again again again');14});15root.addEvent('appInit', function(){16    console.log('appInit event fired again again again again');17});18root.addEvent('appInit', function(){19    console.log('appInit event fired again again again again again');20});21root.addEvent('appInit', function(){22    console.log('appInit event fired again again again again again again');23});24root.addEvent('appInit', function(){25    console.log('appInit event fired again again again again again again again');26});27root.addEvent('appInit', function(){28    console.log('appInit event fired again again again again again again again again');29});30root.addEvent('appInit', function(){31    console.log('appInit event fired again again again again again again again again again');32});33root.addEvent('appInit', function(){34    console.log('appInit event fired again againUsing AI Code Generation
1var root = require('Root');2root.registerListeners();3var Root = function() {4}5Root.prototype.registerListeners = function() {6}7module.exports = new Root();8module.exports = Root;9var root = require('Root');10root.registerListeners();11var Root = function() {12}13Root.prototype.registerListeners = function() {14}15var root = require('Root');16root.registerListeners();17var Root = function() {18}19Root.prototype.registerListeners = function() {20}21module.exports = Root;Using AI Code Generation
1var root = require('./root');2root.registerListeners();3module.exports = function() {4};5var root = require('./root');6root();Using AI Code Generation
1var root = require('./root');2var listener = function() { console.log('event'); };3root.registerListeners(listener);4module.exports = new Root();5var root = new Root();6module.exports = root;Using AI Code Generation
1var rootModule = require("rootModule");2rootModule.registerListeners();3var testModule = (function () {4    var button = document.getElementById("btn");5    var count = 0;6    var onButtonClick = function () {7        console.log("Clicked " + count + " times");8        count++;9    }10    var registerListeners = function () {11        button.addEventListener("click", onButtonClick);12    }13    return {14    };15})();16