Skip to content

Instantly share code, notes, and snippets.

@egnedko
Last active December 14, 2016 15:09
Show Gist options
  • Save egnedko/0de5eaf2780b1f1bac81402b7f50df57 to your computer and use it in GitHub Desktop.
Save egnedko/0de5eaf2780b1f1bac81402b7f50df57 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
<style>
.log{
white-space: pre-wrap; /* Since CSS 2.1 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
</style>
</head>
<body>
<!-- put markup and other contents here -->
<div class="log">
</div>
</body>
</html>
import { normalize, Schema, arrayOf, valueOf } from 'normalizr';
function handleTime(output, key, value, input) {
//console.log('| output => ', output, '| key => ', key, '| value => ', value, '| input => ', input)
if (key === 'start' || key === 'end') {
delete output[key];
}
}
function handleAct(output, key, value, input) {
let repValue = value;
if( key === 'days') {
let originalValue = input[key];
const filterActivities = (output, {activityId, start, end}) => {
output[activityId] = {start, end};
return output;
};
const ln = (output, {date, activities}) => {
activities = activities.reduce(filterActivities, {});
output[date] = {activities}
return output;
}
repValue = originalValue.reduce(ln, {});
}
output[key] = repValue;
}
function handleDate(output, key, value, input) {
let repValue = value;
if(key === 'activities') {
delete output[key];
}
}
const week = new Schema('week', { idAttribute: 'week'});
const user = new Schema('users', { idAttribute: 'userId', assignEntity: handleAct });
const day = new Schema('days', { idAttribute: 'date', assignEntity: handleDate });
const activity = new Schema('activities', { idAttribute: 'activityId', assignEntity: handleTime });
week.define({
users: arrayOf(user)
})
user.define({
days: arrayOf(day)
})
day.define({
activities: arrayOf(activity)
})
const JSON_RESPONSE = {
"week": 32,
"users":
[
{
"userId": 14534,
"firstName": "Dave",
"lastName": "Tidy",
"contractedHours": 37.5,
"plannedHours": 25.5,
"rotaWeek": 1,
"days":
[
{
"date": "2017-01-23",
"day":"Monday",
"activities":
[
{
"activityId": "23",
"activityName": "Deli",
"activityTypeId": 1,
"activityTypeName": "Work",
"start": "09:00",
"end": "11:30"
},
{
"activityId": "30",
"activityName": "Paid Break",
"activityTypeId": 4,
"activityTypeName": "Break",
"start": "11:30",
"end": "12:00"
},
{
"activityId": "25",
"activityName": "Tills",
"activityTypeId": 1,
"activityTypeName": "Work",
"start": "12:00",
"end": "15:00"
}
]
}
]
},
{
"userId": 1453124,
"firstName": "Dave",
"lastName": "Tidy",
"contractedHours": 37.5,
"plannedHours": 25.5,
"rotaWeek": 1,
"days":
[
{
"date": "2017-01-25",
"day":"Monday",
"activities":
[
{
"activityId": "23",
"activityName": "Deli",
"activityTypeId": 1,
"activityTypeName": "Work",
"start": "09:00",
"end": "11:30"
},
{
"activityId": "30",
"activityName": "Paid Break",
"activityTypeId": 4,
"activityTypeName": "Break",
"start": "11:30",
"end": "12:00"
},
{
"activityId": "25",
"activityName": "Tills",
"activityTypeId": 1,
"activityTypeName": "Work",
"start": "12:00",
"end": "15:00"
}
]
}
]
}
]
}
const response = normalize(JSON_RESPONSE, week);
console.log( response )
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"normalizr": "2.3.0"
}
}
'use strict';
var _normalizr = require('normalizr');
function handleTime(output, key, value, input) {
//console.log('| output => ', output, '| key => ', key, '| value => ', value, '| input => ', input)
if (key === 'start' || key === 'end') {
delete output[key];
}
}
function handleAct(output, key, value, input) {
var repValue = value;
if (key === 'days') {
(function () {
var originalValue = input[key];
var filterActivities = function filterActivities(output, _ref) {
var activityId = _ref.activityId;
var start = _ref.start;
var end = _ref.end;
output[activityId] = { start: start, end: end };
return output;
};
var ln = function ln(output, _ref2) {
var date = _ref2.date;
var activities = _ref2.activities;
activities = activities.reduce(filterActivities, {});
output[date] = { activities: activities };
return output;
};
repValue = originalValue.reduce(ln, {});
})();
}
output[key] = repValue;
}
function handleDate(output, key, value, input) {
var repValue = value;
if (key === 'activities') {
delete output[key];
}
}
var week = new _normalizr.Schema('week', { idAttribute: 'week' });
var user = new _normalizr.Schema('users', { idAttribute: 'userId', assignEntity: handleAct });
var day = new _normalizr.Schema('days', { idAttribute: 'date', assignEntity: handleDate });
var activity = new _normalizr.Schema('activities', { idAttribute: 'activityId', assignEntity: handleTime });
week.define({
users: (0, _normalizr.arrayOf)(user)
});
user.define({
days: (0, _normalizr.arrayOf)(day)
});
day.define({
activities: (0, _normalizr.arrayOf)(activity)
});
var JSON_RESPONSE = {
"week": 32,
"users": [{
"userId": 14534,
"firstName": "Dave",
"lastName": "Tidy",
"contractedHours": 37.5,
"plannedHours": 25.5,
"rotaWeek": 1,
"days": [{
"date": "2017-01-23",
"day": "Monday",
"activities": [{
"activityId": "23",
"activityName": "Deli",
"activityTypeId": 1,
"activityTypeName": "Work",
"start": "09:00",
"end": "11:30"
}, {
"activityId": "30",
"activityName": "Paid Break",
"activityTypeId": 4,
"activityTypeName": "Break",
"start": "11:30",
"end": "12:00"
}, {
"activityId": "25",
"activityName": "Tills",
"activityTypeId": 1,
"activityTypeName": "Work",
"start": "12:00",
"end": "15:00"
}]
}]
}, {
"userId": 1453124,
"firstName": "Dave",
"lastName": "Tidy",
"contractedHours": 37.5,
"plannedHours": 25.5,
"rotaWeek": 1,
"days": [{
"date": "2017-01-25",
"day": "Monday",
"activities": [{
"activityId": "23",
"activityName": "Deli",
"activityTypeId": 1,
"activityTypeName": "Work",
"start": "09:00",
"end": "11:30"
}, {
"activityId": "30",
"activityName": "Paid Break",
"activityTypeId": 4,
"activityTypeName": "Break",
"start": "11:30",
"end": "12:00"
}, {
"activityId": "25",
"activityName": "Tills",
"activityTypeId": 1,
"activityTypeName": "Work",
"start": "12:00",
"end": "15:00"
}]
}]
}]
};
var response = (0, _normalizr.normalize)(JSON_RESPONSE, week);
console.log(response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment