ecmascript-7

Succinct/concise syntax for 'optional' object keys in ES6/ES7?

有些话、适合烂在心里 提交于 2019-12-03 22:08:37
There are already a lot of cool features in ES6/ES7 for defining Javascript objects. However, the following pattern is common in Javascript: const obj = { requiredKey1: ..., requiredKey2: ... }; if (someCondition) { obj.optionalKey1 = ...; } Is there a way to define the object all at once with both optional and required keys? You can use object spread to have an optional property. Note: Object Rest/Spread is a stage 4 proposal for ECMAScript. You might need the babel transform to use it. let flag1 = true; let flag2 = false; const obj = { requiredKey1: 1, requiredKey2: 2, ...(flag1 && {

Check if document exists in mongodb using es7 async/await

时光毁灭记忆、已成空白 提交于 2019-12-03 09:17:17
问题 I'm trying to check if the user with the email provided exists in the collection users , but my function keeps returning undefined for every call. I use es6 and async/await in order to get rid of lots of callbacks. Here's my function (it is inside of a class): async userExistsInDB(email) { let userExists; await MongoClient.connect('mongodb://127.0.0.1:27017/notificator', (err, db) => { if (err) throw err; let collection = db.collection('users'); userExists = collection.find({email: email})

Using a Decorator to get list of implemented interfaces

落爺英雄遲暮 提交于 2019-12-03 02:53:58
Do you know if it is possible to get the array of interfaces implemented by a class using a decorator: interface IWarrior { // ... } interface INinja { // ... } So If I do something like: @somedecorator class Ninja implements INinja, IWarrior { // ... } At run-time Ninja will have an annotation which contains ["INinja", "IWarrior"] ? Thanks Currently, types are used only during development and compile time. The type information is not translated in any way to the compiled JavaScript code. But you however can pass the list of strings to the decorator parameter like this: interface IWarrior { //

Check if document exists in mongodb using es7 async/await

浪子不回头ぞ 提交于 2019-12-02 23:29:21
I'm trying to check if the user with the email provided exists in the collection users , but my function keeps returning undefined for every call. I use es6 and async/await in order to get rid of lots of callbacks. Here's my function (it is inside of a class): async userExistsInDB(email) { let userExists; await MongoClient.connect('mongodb://127.0.0.1:27017/notificator', (err, db) => { if (err) throw err; let collection = db.collection('users'); userExists = collection.find({email: email}).count() > 0; console.log(userExists); db.close(); }); console.log(userExists); return userExists; } So,

Why is Math.pow() (sometimes) not equal to ** in JavaScript?

本秂侑毒 提交于 2019-12-02 14:46:25
I've just discovered the ECMAScript 7 feature a**b as an alternative for Math.pow(a,b) ( MDN Reference ) and came across a discussion in that post , in which they apparently behave differently. I've tested it in Chrome 55 and can confirm that the results differ. Math.pow(99,99) returns 3.697296376497263e+197 whereas 99**99 returns 3.697296376497268e+197 So logging the difference Math.pow(99,99) - 99**99 results in -5.311379928167671e+182 . So far it could be said, that it's simply another implementation, but wrapping it in a function behaves different again: function diff(x) { return Math.pow

Is a JavaScript function call a LeftHandSideExpression, thus an ExpressionStatement?

99封情书 提交于 2019-12-02 02:06:01
问题 I'm trying to prove that a simple function call such as window.alert(); is valid EcmaScript 2016 (7th Edition) grammar. Working backward, with the expectation this is an ExpressionStatement , I see that it fits the pattern MemberExpression Arguments which is a CallExpression . And, section 12.3 defines LeftHandSideExpression as possibly a CallExpression . Now, my problem is that section 12.15 AssignmentExpression seems to require that LeftHandSideExpression be followed by either an

How can I use ES2016 (ES7) async/await in my acceptance tests for a Koa.js app?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 22:51:17
I am in the process of writing my first Koa.js app, and having recently been introduced to the ES2016 (aka ES7) features of async / await , I wanted to make use of these. I found that my Google skills were not up to the task, and the few snippets of code I could find were either for the standard Koa (using generators) or otherwise not-as-bleeding-edge-as-ES7. See my answer below for how I got my tests running. I'm still a beginner, so it's likely that a lot of this can be optimised considerably, but here's what worked for me. I'll basically just dump my files here, they should be fairly

Redux - How to add entry to array in reducer

别等时光非礼了梦想. 提交于 2019-11-30 11:22:17
I stuck with this bit and I can't progress - I guess solution is simple but I can't figure out. I'm trying to add entry in reducer so data in in would look something this: state = { entryId: { entryName: ["something", "something2", "something3" /* and so on... */] } }; So far this is the closest I get, but, instead of adding new unique entry, it is replacing the one that is stored already. Also I need to be able to add this item to empty state where entryId, entryName doesn't exist yet to avoid error: switch(type) { case ADD_ENTRY: return { ...state, [entryId]: { ...state[entryId], [entryName]

Redux - How to add entry to array in reducer

孤街醉人 提交于 2019-11-29 17:08:08
问题 I stuck with this bit and I can't progress - I guess solution is simple but I can't figure out. I'm trying to add entry in reducer so data in in would look something this: state = { entryId: { entryName: ["something", "something2", "something3" /* and so on... */] } }; So far this is the closest I get, but, instead of adding new unique entry, it is replacing the one that is stored already. Also I need to be able to add this item to empty state where entryId, entryName doesn't exist yet to

Browser support for array.includes and alternatives

放肆的年华 提交于 2019-11-29 11:47:29
问题 I looked it up and found this regarding finding a substring in a larger string in an array. Array.Prototype.includes if (t.title.includes(searchString)) My t is part of a $.each that's iterating through a larger array of objects (each objects got a buttload of info, from strings, dates and such). searchString is whatever the user typed in a box. All this is a simple search function for a list I have on the page. This works just fine in Chrome. But Firefox and IE are turning up errors stating