Skip to content

Instantly share code, notes, and snippets.

@xalakox
Last active April 24, 2017 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xalakox/8c4a333b31abf6b54af3f89f79c10bd7 to your computer and use it in GitHub Desktop.
Save xalakox/8c4a333b31abf6b54af3f89f79c10bd7 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 -->
</head>
<body>
<!-- put markup and other contents here -->
</body>
</html>
// Write ES2015 code and import modules from npm
// and then press "Execute" to run your program.
/*
Please write a function that checks if two objects have the same content.
*/
/* NOTES:
1. Ignore _id.
2. Keys in the objects are unknown to this function.
Please don't do:
offerA.rate equals offerB.rate and
offerA.offerType equals offerB.offerType
...etc
3. Write smaller functions if necessary and name them well.
4. Write comments if necessary.
5. Below are the two objects we want you to compare and the result should be true.
6. WHEN COMPLETE, ACTIONS -> SAVE PRIVATE GIST, AND INCLUDE THE LINK IN THE WRITTEN INTERVIEW.
*/
// Optional: Checkout Ramda. It has already been imported in this module for you.
import R from 'ramda';
const offerA = {
rate: "freeOfCharge",
bookingCode: "FREELOCAL",
allowCombination: false,
offerName: "Bring a Local for FREE! Promotion",
specialConditions: "Please note, this special offer can be booked today, and is valid for travel at any time during the year except for December 24th through January 6th.",
offerType: "Per person",
_id: "57f7019e18f4fc0300a9e6d1",
discountType: "percentage",
validDatesOfTravel: [
{
fromDate: "2017-04-01T00:00:00.000Z",
toDate: "2017-12-24T00:00:00.000Z",
_id: "123",
updatedAt: "2016-06-28T21:20:21.754Z",
createdAt: "2016-06-28T21:20:21.754Z"
},
{
_id: "57f7019e18f4fc0300a9e6d3",
fromDate: "2018-01-06T00:00:00.000Z",
toDate: "2018-03-31T00:00:00.000Z",
updatedAt: "2016-08-17T20:21:08.723Z",
createdAt: new Date("2016-08-17T20:21:08.723Z")
}
],
tags: ['free', 'multi'],
rates: [{nett: 2, commission: '20%'}]
};
const offerB = {
_id: "57f7019e18f4fc0300a9e6d1",
rate: "freeOfCharge",
bookingCode: "FREELOCAL",
allowCombination: false,
offerName: "Bring a Local for FREE! Promotion",
specialConditions: "Please note, this special offer can be booked today, and is valid for travel at any time during the year except for December 24th through January 6th.",
offerType: "Per person",
discountType: "percentage",
validDatesOfTravel: [
{
fromDate: "2018-01-06T00:00:00.000Z",
_id: "57f7019e18f4fc0300a9e6d3",
toDate: "2018-03-31T00:00:00.000Z",
updatedAt: new Date("2016-08-17T20:21:08.723Z"),
createdAt: "2016-08-17T20:21:08.723Z"
},
{
fromDate: "2017-04-01T00:00:00.000Z",
toDate: "2017-12-24T00:00:00.000Z",
updatedAt: "2016-06-28T21:20:21.754Z",
createdAt: "2016-06-28T21:20:21.754Z",
}
],
tags: ['multi', 'free'],
rates: [{commission: '20%', nett: 2}]
};
// YOUR CODE STARTS HERE.
const compare = (a,b)=>{
delete a._id
delete b._id
//convert to regular epoch if it is a date or a string that could be a date
if (Object.prototype.toString.call(a) === '[object Date]'){
console.log(a)
a = a.getTime()
}else{
if (Date.parse(a)){
a = Date.parse(a)
}
}
if (Object.prototype.toString.call(b) === '[object Date]'){
b = b.getTime()
}else{
if (Date.parse(b)){
b = Date.parse(b)
}
}
// types must match
if (typeof a !== typeof b) return false
// regular comparisson
switch (typeof a){
case 'object':{
if (Array.isArray(a)){ // an array
if (a.length !== b.length) { // array size differs
return false
}
for(var i = 0; i < a.length; i++) {
if (typeof a[i] === "object"){ // array of objects, must compare one by one
let found = false
for (var j = 0; j < b.length; j++){
if (compare(a[i],b[j])){
found = true;
break;
}
}
if (!found){ //a[i] not found in b
return false;
}
}else{
// array of primitives (strings or numbers)
if (b.indexOf(a[i]) < 0 ){ // a[i] not contained on b
return false
}
}
}
}else{ // an object, we have to walk all attributes
for(var i = 0; i < Object.keys(a).length; i++) {
const key = Object.keys(a)[i]
if (!compare(a[key],b[key])) return false
}
}
break;
}
default:{ // primitive comparison
if (a !== b) return false
}
}
return true
}
console.log(compare(offerA,offerB));
//console.log(compare(new Date("2016-08-17T20:21:08.723Z"),'2016-08-17T20:21:08.723Z'))
// WHEN COMPLETE SAVE AS PRIVATE GIST AND INCLUDE THE LINK IN THE WRITTEN INTERVIEW.
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"ramda": "0.23.0",
"babel-runtime": "6.23.0"
}
}
"use strict";
var _keys = require("babel-runtime/core-js/object/keys");
var _keys2 = _interopRequireDefault(_keys);
var _typeof2 = require("babel-runtime/helpers/typeof");
var _typeof3 = _interopRequireDefault(_typeof2);
var _ramda = require("ramda");
var _ramda2 = _interopRequireDefault(_ramda);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var offerA = {
rate: "freeOfCharge",
bookingCode: "FREELOCAL",
allowCombination: false,
offerName: "Bring a Local for FREE! Promotion",
specialConditions: "Please note, this special offer can be booked today, and is valid for travel at any time during the year except for December 24th through January 6th.",
offerType: "Per person",
_id: "57f7019e18f4fc0300a9e6d1",
discountType: "percentage",
validDatesOfTravel: [{
fromDate: "2017-04-01T00:00:00.000Z",
toDate: "2017-12-24T00:00:00.000Z",
_id: "123",
updatedAt: "2016-06-28T21:20:21.754Z",
createdAt: "2016-06-28T21:20:21.754Z"
}, {
_id: "57f7019e18f4fc0300a9e6d3",
fromDate: "2018-01-06T00:00:00.000Z",
toDate: "2018-03-31T00:00:00.000Z",
updatedAt: "2016-08-17T20:21:08.723Z",
createdAt: new Date("2016-08-17T20:21:08.723Z")
}],
tags: ['free', 'multi'],
rates: [{ nett: 2, commission: '20%' }]
}; // Write ES2015 code and import modules from npm
// and then press "Execute" to run your program.
/*
Please write a function that checks if two objects have the same content.
*/
/* NOTES:
1. Ignore _id.
2. Keys in the objects are unknown to this function.
Please don't do:
offerA.rate equals offerB.rate and
offerA.offerType equals offerB.offerType
...etc
3. Write smaller functions if necessary and name them well.
4. Write comments if necessary.
5. Below are the two objects we want you to compare and the result should be true.
6. WHEN COMPLETE, ACTIONS -> SAVE PRIVATE GIST, AND INCLUDE THE LINK IN THE WRITTEN INTERVIEW.
*/
// Optional: Checkout Ramda. It has already been imported in this module for you.
var offerB = {
_id: "57f7019e18f4fc0300a9e6d1",
rate: "freeOfCharge",
bookingCode: "FREELOCAL",
allowCombination: false,
offerName: "Bring a Local for FREE! Promotion",
specialConditions: "Please note, this special offer can be booked today, and is valid for travel at any time during the year except for December 24th through January 6th.",
offerType: "Per person",
discountType: "percentage",
validDatesOfTravel: [{
fromDate: "2018-01-06T00:00:00.000Z",
_id: "57f7019e18f4fc0300a9e6d3",
toDate: "2018-03-31T00:00:00.000Z",
updatedAt: new Date("2016-08-17T20:21:08.723Z"),
createdAt: "2016-08-17T20:21:08.723Z"
}, {
fromDate: "2017-04-01T00:00:00.000Z",
toDate: "2017-12-24T00:00:00.000Z",
updatedAt: "2016-06-28T21:20:21.754Z",
createdAt: "2016-06-28T21:20:21.754Z"
}],
tags: ['multi', 'free'],
rates: [{ commission: '20%', nett: 2 }]
};
// YOUR CODE STARTS HERE.
var compare = function compare(a, b) {
delete a._id;
delete b._id;
//convert to regular epoch if it is a date or a string that could be a date
if (Object.prototype.toString.call(a) === '[object Date]') {
console.log(a);
a = a.getTime();
} else {
if (Date.parse(a)) {
a = Date.parse(a);
}
}
if (Object.prototype.toString.call(b) === '[object Date]') {
b = b.getTime();
} else {
if (Date.parse(b)) {
b = Date.parse(b);
}
}
// types must match
if ((typeof a === "undefined" ? "undefined" : (0, _typeof3.default)(a)) !== (typeof b === "undefined" ? "undefined" : (0, _typeof3.default)(b))) return false;
// regular comparisson
switch (typeof a === "undefined" ? "undefined" : (0, _typeof3.default)(a)) {
case 'object':
{
if (Array.isArray(a)) {
// an array
if (a.length !== b.length) {
// array size differs
return false;
}
for (var i = 0; i < a.length; i++) {
if ((0, _typeof3.default)(a[i]) === "object") {
// array of objects, must compare one by one
var found = false;
for (var j = 0; j < b.length; j++) {
if (compare(a[i], b[j])) {
found = true;
break;
}
}
if (!found) {
//a[i] not found in b
return false;
}
} else {
// array of primitives (strings or numbers)
if (b.indexOf(a[i]) < 0) {
// a[i] not contained on b
return false;
}
}
}
} else {
// an object, we have to walk all attributes
for (var i = 0; i < (0, _keys2.default)(a).length; i++) {
var key = (0, _keys2.default)(a)[i];
if (!compare(a[key], b[key])) return false;
}
}
break;
}
default:
{
// primitive comparison
if (a !== b) return false;
}
}
return true;
};
console.log(compare(offerA, offerB));
//console.log(compare(new Date("2016-08-17T20:21:08.723Z"),'2016-08-17T20:21:08.723Z'))
// WHEN COMPLETE SAVE AS PRIVATE GIST AND INCLUDE THE LINK IN THE WRITTEN INTERVIEW.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment