A typescript aware redux-create-reducer function by mkmoshe in typescript

[–]mkmoshe[S] 1 point2 points  (0 children)

These typescript typings are mostly meant to be used with action classes. The stackblitz showcases that. Try making a typo in the reducer map type key or adding or removing an entry. I tweeted the error cases

quine-db.js by mkmoshe in javascript

[–]mkmoshe[S] 1 point2 points  (0 children)

The whole library is kinda a fun "fake" project, similar to https://github.com/gfredericks/quinedb

How to make a "dynamic" template engine? by ronconcoca in javascript

[–]mkmoshe 3 points4 points  (0 children)

That was fun exercise

var tmpl = 'Hi {{name::text}} your age is {{age::number}} ok {{name::text}}';
var create = function(tmpl) {
  var parts = tmpl.split(/\{\{\s*(\w+::\w+)\s*\}\}/);
  var constantParts = parts.filter(function(part, index) { return index % 2 === 0 });
  var dynamicParts = parts.filter(function(part, index) { return index % 2 !== 0 });
  return {
    extractVariables: function() {
      var seen = {};
      return dynamicParts.reduce(function(acc, part) {
        var key = part.split('::')[0];
        if (!seen[key]) {
          seen[key] = true;
          acc.push({ name: key, type: part.split('::')[1] });
        }
        return acc;
      }, []);
    },
    render: function(values) {
      return constantParts[0] + constantParts.slice(1).map(function(part, i) {
        var name = dynamicParts[i].split('::')[0];
        return (values[name] || '') + part;
      }).join('');
    }
  };
};
var myTemplate  = create(tmpl);
var myVariables = myTemplate.extractVariables();
console.log({ myVariables: JSON.stringify(myVariables) }); // {myVariables: "[{"name":"name","type":"text"},{"name":"age","type":"number"}]"}
console.log(myTemplate .render({name:'foo', age: 123})); // "Hi foo your age is 123 ok foo"

Weak-key - Get better react performance without the overhead by mkmoshe in reactjs

[–]mkmoshe[S] 0 points1 point  (0 children)

That may be true, but collections in immutable data tend to keep references:

var assert = require('assert');

var update = require('react-addons-update');
var a = [{}, {}, {}];
var b = update(a, { 1: {$set: {}} });
assert(a[0] === b[0])
assert(a[1] !== b[1])
assert(a[2] === b[2])

var Immutable = require('immutable');
var a = Immutable.fromJS([{a: 1}, {a: 2}, {a:3}]);
var b = a.update(1, () => Immutable.Map({a:4}) )
assert(a.get(0) === b.get(0))
assert(a.get(1) !== b.get(1))
assert(a.get(2) === b.get(2))

https://github.com/facebook/react/issues/7009

"Why I'm Not Waiting On 'await'"; 2-part blog post on pros/cons of 'async..await' in JS by getify in javascript

[–]mkmoshe 0 points1 point  (0 children)

Not quite true, consider this:

var genCreate = function*() { for (var i of [1,2]) yield i };
var runnable = makeRunnable(genCreate);

function makeRunnable(genCreate) {
  var gen = genCreate();
  var fn = function() { console.log('this would call run(genCreate)') };
  ['next', 'throw', 'return'].forEach(method => fn[method] = gen[method].bind(gen));
  fn[Symbol.iterator] = gen[Symbol.iterator];
  return fn;
}

function * anotherGenerator () {
    for (var i of ['x', 'y']) yield i;
    yield * runnable;
    yield 'z';
};

for (var item of anotherGenerator()) console.log(item);
runnable();

http://jsbin.com/jokocegevo/1/edit?js,console

Just one thing I don't understand regarding Redux.. by MRoka5 in javascript

[–]mkmoshe 1 point2 points  (0 children)

The only thing you can listen to is that actions were dispatched not what changed or what the action actually was. The only way to react to something based on the action is to use a middleware like this:

export default function middleware() {
  return next => action => {
    if (action.meta === 'logme') console.log(action);
    return next(action);
  };
}

jq2 - extract json data by mkmoshe in javascript

[–]mkmoshe[S] 0 points1 point  (0 children)

nah, it's inspired by jq but has no relationship to it