Skip to content

Instantly share code, notes, and snippets.

@freefony
Created November 25, 2019 09:33
Show Gist options
  • Save freefony/4033326ae834c5ecf554dec95778c1ef to your computer and use it in GitHub Desktop.
Save freefony/4033326ae834c5ecf554dec95778c1ef to your computer and use it in GitHub Desktop.
'use strict'
const deepClone = (obj) => {
let copyObj
// if arg is primitive return
if (Object(obj) !== obj) return obj
copyObj = obj.constructor()
for (let key in obj) {
if (!obj.hasOwnProperty(key)) continue
const value = obj[key]
switch (true) {
case (value instanceof Date):
copyObj[key] = new Date()
copyObj[key].setDate(value.getDate())
break
case (value instanceof Function):
copyObj[key] = obj[key]
Object.defineProperty(copyObj[key], "name", { value: value.name });
break
case (value instanceof Array):
case (value instanceof Object):
copyObj[key] = deepClone(value)
break
default:
copyObj[key] = obj[key]
}
}
return copyObj
}
module.exports = {
deepClone
}
'use strict'
const { deepClone } = require('./deepclone')
describe('deepClone', () => {
it ('should clone properties of source object', () => {
const srcObj = {
string: 'test',
array: ['1'],
date: new Date(),
object:{name: "Paddy", address: {town: "Lerum", country: "Sweden"}},
function: function jirt(){
return this.date
}
}
const targetObj = deepClone(srcObj)
expect(targetObj).toMatchObject(srcObj)
expect(targetObj.string).toEqual(srcObj.string)
expect(targetObj.array).toEqual(srcObj.array)
expect(targetObj.date).toEqual(srcObj.date)
expect(targetObj.object).toMatchObject(srcObj.object)
expect(targetObj.function()).toEqual(srcObj.function())
})
})
'use strict'
class GetListToInvite {
constructor (startingPoint, maxDistance = 100) {
this.maxDistance = 100
this.officeLocation = startingPoint
}
degrees_to_radians(degrees) {
var pi = Math.PI
return degrees * (pi / 180)
}
_haveOfficeWithinDistance (partner) {
const officesWithin = partner.offices.filter(({ coordinates }) => {
const latLong = coordinates.split(',')
const to = { latitude: latLong[0], longitude: latLong[1] }
const distBtw = this.getDistanceBetween(this.officeLocation, to)
return distBtw < this.maxDistance
})
return officesWithin.length
}
getDistanceBetween (from, to, decimals = 2) {
const earthRadius = 6371 // km
let lat1 = parseFloat(from.latitude)
let lat2 = parseFloat(to.latitude)
const lon1 = parseFloat(from.longitude)
const lon2 = parseFloat(to.longitude)
const dLat = this.degrees_to_radians(lat2 - lat1)
const dLon = this.degrees_to_radians(lon2 - lon1)
lat1 = this.degrees_to_radians(lat1)
lat2 = this.degrees_to_radians(lat2)
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
var d = earthRadius * c
return Math.round(d * Math.pow(10, decimals)) / Math.pow(10, decimals)
}
compareDistance (partners) {
return partners.filter(this._haveOfficeWithinDistance.bind(this))
}
}
module.exports = { GetListToInvite }
'use strict'
const { GetListToInvite } = require('./distance')
const partners = require('./partners.json')
describe('Distance between coordinates', () => {
const officeLocation = { latitude: '51.515419', longitude: '-0.141099' }
const getListToInvite = new GetListToInvite(officeLocation)
const invited = getListToInvite.compareDistance(partners)
it('should return list of those invited', () => {
expect(invited).toBeTruthy()
})
it('should filter out office outside the distance', () => {
expect(invited.length).toBeLessThan(partners.length)
})
})
{
"name": "spidergap",
"version": "1.0.0",
"description": "",
"main": "deep-clone.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"jest": "^24.9.0",
"tape": "^4.11.0"
},
"jest": {
"testEnvironment": "node",
"coveragePathIgnorePatterns": [
"/node_modules/"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment