Git Product home page Git Product logo

tween's Introduction

tween's People

Contributors

nuintun avatar

Watchers

 avatar

tween's Issues

Events.js

/**
 * @module events
 * @license MIT
 * @author nuintun
 */

import * as Utils from './utils';

/**
 * @class Events
 * @constructor
 */
export default function Events() {
  this._events = {};
}

// Set prototype
Events.prototype = {
  /**
   * @method on
   * @description Bind callback tos event
   * @param {string} events
   * @param {Function} callback
   * @param {any} context
   * @returns {Events}
   */
  on: function(event, callback) {
    var context = this;
    var events = context._events;
    var callbacks = events[event] || (events[event] = []);

    if (Utils.typeIs(callback, 'Function')) {
      callbacks.push(callback);
    }

    return context;
  },
  /**
   * @method off
   * @description Remove callback of event
   *   If `callback` is null, removes all callbacks for the event.
   *   If `event` is null, removes all bound callbacks for the event.
   * @param {string} event
   * @param {Function} callback
   * @returns {Events}
   */
  off: function(event, callback) {
    var context = this;

    switch (arguments.length) {
      case 0:
        context._events = {};
        break;
      case 1:
        delete context._events[event];
        break;
      default:
        var callbacks = events[event];

        if (callbacks) {
          var i = Utils.indexOf(callbacks, callback);

          if (i !== -1) {
            callbacks.splice(i, 1);
          }
        }
        break;
    }

    return context;
  },
  /**
   * @method once
   * @description Bind a event only emit once
   * @param {string} events
   * @param {Function} callback
   * @returns {Events}
   */
  once: function(events, callback) {
    var that = this;

    function feedback() {
      that.off(events, feedback);
      Utils.apply(callback, this, arguments);
    }

    return that.on(events, feedback);
  },
  /**
   * @method emitWith
   * @description Emit event with context
   * @param {string} event
   * @param {Array} args
   * @param {any} context
   * @returns {Events}
   */
  emitWith: function(event, args, context) {
    var that = this;
    var events = that._events;
    var callbacks = events[event];

    if (!callbacks || callbacks.length === 0) {
      return false;
    }

    // Arguments length
    var length = arguments.length;

    // Default context
    if (length < 3) {
      context = that;
    }

    // Format args
    if (length < 2) {
      args = [];
    } else {
      args = Utils.typeIs(args, 'Array') ? args : [args];
    }

    Utils.forEach(callbacks, function(callback) {
      Utils.apply(callback, context, args);
    });

    return true;
  },
  /**
   * @method emit
   * @description Emit event
   * @param {string} event
   * @param {any} [...args]
   * @returns {Events}
   */
  emit: function(event) {
    var context = this;
    var events = context._events;
    var callbacks = events[event];

    if (!callbacks || callbacks.length === 0) {
      return false;
    }

    var rest = [];

    // Fill up `rest` with the callback arguments. Since we're only copying
    // the tail of `arguments`, a loop is much faster than Array#slice.
    for (var i = 1, length = arguments.length; i < length; i++) {
      rest[i - 1] = arguments[i];
    }

    return context.emitWith(event, rest);
  }
};

小数精确相加

  /**
   * @function add
   * @param {number} x
   * @param {number} y
   * @returns {number}
   */
  function add(x, y) {
    var decimal = /\.\d+/;
    var x1 = decimal.exec(String(x));
    var y1 = decimal.exec(String(y));
    var e = Math.max(x1 ? x1[0].length - 1 : 0, y1 ? y1[0].length - 1 : 0);

    if (e) {
      e = Math.pow(10, e);

      return (x * e + y * e) / e;
    } else {
      return x + y;
    }
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.