Skip to content
This repository has been archived by the owner on Nov 21, 2019. It is now read-only.

Commit

Permalink
Fix indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
johnste committed Aug 23, 2016
1 parent 8ed4a14 commit dd2a33b
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 279 deletions.
90 changes: 45 additions & 45 deletions lib/getify.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory)
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory()
} else {
// Browser globals (root is window)
root.getify = factory()
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory)
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory()
} else {
// Browser globals (root is window)
root.getify = factory()
}
}(this, function () {
"use strict"
"use strict"

function getPath(obj, path = []) {
function getPath(obj, path = []) {
const length = path.length
let hasOwn = true
let key

return (defaultValue) => {
return (defaultValue) => {
let index = 0
while (obj != null && index < length) {
key = path[index++]
Expand All @@ -35,50 +35,50 @@
}

return (hasOwn && index === length) ? obj : defaultValue;
}
}
}
}

function getify(obj) {
function getify(obj) {

function handler(path) {
return {
get: function(target, key){
// If the prototype request it, we return it, since
// not returning it causes issues while outputting stuff
// to console, for example
if (obj[key] && obj[key].isPrototypeOf(obj)) {
return obj[key]
}
function handler(path) {
return {
get: function(target, key){
// If the prototype request it, we return it, since
// not returning it causes issues while outputting stuff
// to console, for example
if (obj[key] && obj[key].isPrototypeOf(obj)) {
return obj[key]
}

const newPath = [...path, key]
const newPath = [...path, key]
return new Proxy(getPath(obj, newPath), handler(newPath))
}
}
}
}
}
}

// For consistency's sake, we return the root object as a function
// For consistency's sake, we return the root object as a function
return new Proxy(getPath(obj), handler([]))
}
}

// Create utility symbols
getify.all = Symbol("getify.all")
getify.first = Symbol("getify.first")
getify.last = Symbol("getify.last")
// Create utility symbols
getify.all = Symbol("getify.all")
getify.first = Symbol("getify.first")
getify.last = Symbol("getify.last")

const operators = {
[getify.all]: (obj, path, defaultValue) => {
const operators = {
[getify.all]: (obj, path, defaultValue) => {
return Object.keys(obj).map(key => getPath(obj[key], path)(defaultValue))
},
[getify.first]: (obj, path, defaultValue) => {
[getify.first]: (obj, path, defaultValue) => {
const firstKey = Object.keys(obj)[0]
return getPath(obj[firstKey], path)(defaultValue)
},
[getify.last]: (obj, path, defaultValue) => {
const keys = Object.keys(obj)
const lastKey = keys[keys.length - 1]
[getify.last]: (obj, path, defaultValue) => {
const keys = Object.keys(obj)
const lastKey = keys[keys.length - 1]
return getPath(obj[lastKey], path)(defaultValue)
}
}
}
}

return getify
return getify
}));
Loading

0 comments on commit dd2a33b

Please sign in to comment.