GithubHelp home page GithubHelp logo

feathersjs-ecosystem / commons Goto Github PK

View Code? Open in Web Editor NEW
11.0 11.0 10.0 262 KB

[MOVED] Shared utility functions

Home Page: https://github.com/feathersjs/feathers

License: MIT License

JavaScript 100.00%

commons's People

Contributors

beeplin avatar bertho-zero avatar corymsmith avatar daffl avatar ekryski avatar greenkeeper[bot] avatar greenkeeperio-bot avatar kenany avatar lopezjurip avatar marshallswain avatar sylvainlap avatar vonagam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

commons's Issues

Rename hooks to hookUtils to make room for common hooks.

What do y'all think about renaming the current hooks export to hookUtils so we can create a hooks folder and starting putting common hooks in it? What modules are using it? Just feathers-hooks or is there anything else we'd have to update?

getArguments not exporting correctly

This was working for me before the latest commit, but I'm getting the following error, now:

430[{"stack":"TypeError: undefined is not a function
    at Object.mixin.(anonymous function) (node_modules/feathers/lib/mixins/normalizer.js:11:22)
    at Object.self.(anonymous function) [as find] (node_modules/feathers/node_modules/uberproto/lib/proto.js:64:21)
    at Socket.<anonymous> (node_modules/feathers/node_modules/feathers-commons/lib/sockets/helpers.js:77:25)
    at Socket.emit (events.js:98:17)
    at Socket.onevent (node_modules/feathers/node_modules/socket.io/lib/socket.js:327:8)
    at Socket.onpacket (node_modules/feathers/node_modules/socket.io/lib/socket.js:287:12)
    at Client.ondecoded (node_modules/feathers/node_modules/socket.io/lib/client.js:193:14)
    at Decoder.Emitter.emit (node_modules/feathers/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)
    at Decoder.add (node_modules/feathers/node_modules/socket.io/node_modules/socket.io-parser/index.js:247:12)
    at Client.ondata (node_modules/feathers/node_modules/socket.io/lib/client.js:175:18)","arguments":["undefined"],"type":"called_non_callable","message":"undefined is not a function"}]

A console.log of the require("./arguments") line here is resulting in the following output:

{ default: [Function: getArguments],
  converters: 
   { find: [Function: find],
     create: [Function: create],
     update: [Function],
     patch: [Function],
     get: [Function],
     remove: [Function] } }

Here is the compiled output for arguments.js

"use strict";

var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };

exports["default"] = getArguments;

var _ = _interopRequire(require("lodash"));

var getCallback = function (args) {
  return typeof _.last(args) === "function" ? _.last(args) : _.noop;
};
var getParams = function (args, position) {
  return typeof args[position] === "object" ? args[position] : {};
};

var updateOrPatch = function (name) {
  return function (args) {
    var id = args[0];
    var data = args[1];
    var callback = getCallback(args);
    var params = getParams(args, 2);

    if (typeof id === "function") {
      throw new Error("First parameter for '" + name + "' can not be a function");
    }

    if (typeof data !== "object") {
      throw new Error("No data provided for '" + name + "'");
    }

    if (args.length > 4) {
      throw new Error("Too many arguments for '" + name + "' service method");
    }

    return [id, data, params, callback];
  };
};

var getOrRemove = function (name) {
  return function (args) {
    var id = args[0];
    var params = getParams(args, 1);
    var callback = getCallback(args);

    if (args.length > 3) {
      throw new Error("Too many arguments for '" + name + "' service method");
    }

    if (id === "function") {
      throw new Error("First parameter for '" + name + "' can not be a function");
    }

    return [id, params, callback];
  };
};

var converters = exports.converters = {
  find: function find(args) {
    var callback = getCallback(args);
    var params = getParams(args, 0);

    if (args.length > 2) {
      throw new Error("Too many arguments for 'find' service method");
    }

    return [params, callback];
  },

  create: function create(args) {
    var data = args[0];
    var params = getParams(args, 1);
    var callback = getCallback(args);

    if (typeof data !== "object") {
      throw new Error("First parameter for 'create' must be an object");
    }

    if (args.length > 3) {
      throw new Error("Too many arguments for 'create' service method");
    }

    return [data, params, callback];
  },

  update: updateOrPatch("update"),

  patch: updateOrPatch("patch"),

  get: getOrRemove("get"),

  remove: getOrRemove("remove")
};

function getArguments(method, args) {
  return converters[method](args);
}

Object.defineProperty(exports, "__esModule", {
  value: true
});

Can we revert your last commit? Or is there a better option? Maybe I'm doing something wrong.

Sort error on multiple fields

Hi,

The sort algoritm keeps taking into account the secondary fields even if the higher precendence fields are differet. This leads to strange results when sorting with multiple fields.

Two considerations:

1st
The algorithm is wrong.
It keeps taking into account less important fields even when the most important fields are already different!

exports.sorter = function sorter ($sort) {
  return function (first, second) {
    let comparator = 0;
    _.each($sort, (modifier, key) => {
      modifier = parseInt(modifier, 10);
        if (first[key] < second[key]) {
          comparator -= 1 * modifier;
        }

        if (first[key] > second[key]) {
          comparator += 1 * modifier;
        }
    });
    return comparator;
  };
};

2nd
The test method on test\utils.test.js is malformed. The test takes into account the 2nd sort field as with higher precedence!

const sort = sorter({
  name: -1,
  counter: 1
});

expect(array.sort(sort)).to.deep.equal([
  { name: 'Eric', counter: 0 },
  { name: 'David', counter: 0 },
  { name: 'Eric', counter: 1 },
  { name: 'David', counter: 1 }
]);

If name is the 1st sort field it should be more important to sort definition than counter.

and only works as expected because the array is too small (coincidence).
Just adding another person. eg. Andrew, the test fails (or adding a third property).

it('two property sorter with three names', () => {
      const array = [{
        name: 'David',
        counter: 0
      }, {
        name: 'Eric',
        counter: 1
      }, {
        name: 'Andrew',
        counter: 1
      }, {
        name: 'David',
        counter: 1
      }, {
        name: 'Andrew',
        counter: 0
      }, {
        name: 'Eric',
        counter: 0
      }];

Cannot build with create-react-app

Hi,

I have those dependencies:

"@feathersjs/feathers": "^3.0.2",
"react-scripts": "1.0.14",

The build command failed to compile files from "commons" module because they are not in es5:

Creating an optimized production build...
Failed to compile.

Failed to minify the code from this file: 

        ./node_modules/@feathersjs/commons/lib/utils.js:8 

Read more here: http://bit.ly/2tRViJ9

error Command failed with exit code 1.

No problem with former feathersjs versions on my project.

Thanks for your help!

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.