feathersjs-ecosystem / commons Goto Github PK
View Code? Open in Web Editor NEW[MOVED] Shared utility functions
Home Page: https://github.com/feathersjs/feathers
License: MIT License
[MOVED] Shared utility functions
Home Page: https://github.com/feathersjs/feathers
License: MIT License
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
}];
It looks as if things like service.create(data)
is not being normalized properly. The error at https://github.com/feathersjs/feathers-commons/blob/master/src/arguments.js#L67 is thrown instead.
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!
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?
For example https://github.com/feathersjs/feathers-commons/blob/master/src/arguments.js#L66 is an easy error to make. It should provide more information like what type was provided instead and what the most common reason for it is (e.v. bodyParser registered after the server).
I am creating rest service that receiving raw url string and it returns error.
First parameter for 'create' must be an object
So, I think Feathers should not prohibit from sending other types of data.
Seeing from the code, it allows only object data type.
https://github.com/feathersjs/feathers-commons/blob/master/src/arguments.js#L66
This should be restricted manually by users or (normally) by body-parser, instead.
Attributed to this line because null
is an object.
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.
In https://github.com/feathersjs/feathers-commons/blob/master/src/sockets/helpers.js#L38, needs to use .call(service
so that those methods can use it.
_.each (which could be an easy ES5 helper in utils.js),
_.extend (which is Object.assign) and and
_.isArray (which is provided by Array.isArray).
As mentioned by @daffl in feathersjs-ecosystem/client#11
We need to move the babel config to a .babelrc
file and then .npmignore it.
I've followed the instructions for using create-react-app and that works, until I introduce feathers-localstorage. Then I get more or less the same error. I'm getting the sense that there's something that needs to happen in feathersjs/commons to fix the issue.
The call to info.clients()
now returns an object. Bad versioning ๐ข
See: https://github.com/feathersjs/feathers-commons/blob/master/src/sockets/helpers.js#L37
Getting the error Uncaught ReferenceError: convertGetOrRemove is not defined
when using Chrome WebView.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.