Skip to content

Commit

Permalink
Refactor getFields
Browse files Browse the repository at this point in the history
  • Loading branch information
gamtiq committed Jan 26, 2016
1 parent 1514dd7 commit b47e439
Show file tree
Hide file tree
Showing 41 changed files with 110,150 additions and 52,027 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = function(grunt) {
src: ["<%= src %>", "README.md"],
options: {
destination: "doc",
template: "node_modules/grunt-jsdoc/node_modules/ink-docstrap/template",
template: "node_modules/ink-docstrap/template",
configure: "jsdoc-conf.json"
}
}
Expand Down
2 changes: 1 addition & 1 deletion LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014-2015 Denis Sikuler
Copyright (c) 2014-2016 Denis Sikuler

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,17 @@ function inc(data) {
var s1 = Symbol("s1"),
s2 = Symbol("s2"),
proto = {a: 1},
obj = Object.create(proto);
obj2 = Object.create(proto),
obj;
proto[s1] = "s1";
obj.b = 2;
obj[s2] = null;
obj2.b = 2;
obj2[s2] = null;
obj2.c = "str";
obj2.d = 0;

adam.getPropertySymbols(obj); // [s2, s1]
adam.getPropertySymbols(obj2); // [s2, s1]
adam.getFields(obj2); // ["b", "c", "d", s2, "a", s1]
adam.getFields(obj2, {filter: ["string", "false"], filterConnect: "or"}); // ["c", "d", s2, s1]

obj = {a: 1, b: 2, c: 3, d: 4, e: 5};

Expand Down Expand Up @@ -246,5 +251,5 @@ Add unit tests for any new or changed functionality.
Lint and test your code using [Grunt](http://gruntjs.com/).

## License
Copyright (c) 2014-2015 Denis Sikuler
Copyright (c) 2014-2016 Denis Sikuler
Licensed under the MIT license.
93 changes: 82 additions & 11 deletions dist/adam.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* adam
* https://github.com/gamtiq/adam
*
* Copyright (c) 2014-2015 Denis Sikuler
* Copyright (c) 2014-2016 Denis Sikuler
* Licensed under the MIT license.
*/

Expand All @@ -33,12 +33,56 @@

/*jshint latedef:nofunc*/

var getPropertySymbols;
var getOwnPropertyNames, getPropertySymbols, getPrototypeOf;

/*jshint laxbreak:true*/
/*
* Return own property names of given object.
*
* @param {Object} obj
* Object whose own property names should be returned.
* @return {Array}
* Own property names of the given object.
*/
getOwnPropertyNames = typeof Object.getOwnPropertyNames === "function"
? Object.getOwnPropertyNames
: function getOwnPropertyNames(obj) {
var result = [],
sKey;
for (sKey in obj) {
result.push(sKey);
}
return result;
};

/*
* Return prototype of given object.
*
* @param {Object} obj
* Object whose prototype should be returned.
* @return {Object}
* The prototype of the given object.
*/
getPrototypeOf = typeof Object.getPrototypeOf === "function"
? Object.getPrototypeOf
: function getPrototypeOf(obj) {
/*jshint proto:true*/
return obj
? (obj.constructor
? obj.constructor.prototype
: (obj.__proto__ || Object.prototype)
)
: null;
};
/*jshint laxbreak:false*/


if (typeof Object.getOwnPropertySymbols === "function") {
/**
* Return list of all symbol property keys for given object including keys from prototype chain.
*
* This function is defined only when `Object.getOwnPropertySymbols` is available.
*
* @param {Object} obj
* Object to be processed.
* @return {Array}
Expand Down Expand Up @@ -68,6 +112,7 @@
};
}


/**
* Return class of given value (namely value of internal property `[[Class]]`).
*
Expand Down Expand Up @@ -295,7 +340,6 @@
/*jshint laxbreak:true*/
var test = true,
bAnd, nI, nL, value;
field = String(field);
if (! settings) {
settings = {};
}
Expand Down Expand Up @@ -325,7 +369,7 @@
}
break;
case "RegExp":
test = test.test(value);
test = test.test(typeof value === "symbol" ? value.toString() : value);
break;
case "Object":
if ("and" in test) {
Expand All @@ -337,10 +381,10 @@
else if ("field" in test) {
test = test.field;
if (getClass(test) === "RegExp") {
test = test.test(field);
test = test.test(typeof field === "symbol" ? field.toString() : field);
}
else {
test = String(test) === field;
test = test === field;
}
}
else if ("value" in test) {
Expand All @@ -363,6 +407,8 @@
/**
* Return list of all or filtered fields of specified object.
*
* Fields are searched (checked) in the object itself and in its prototype chain.
*
* @param {Object} obj
* Object to be processed.
* @param {Object} [settings]
Expand All @@ -378,14 +424,39 @@
* @see {@link module:adam.checkField checkField}
*/
function getFields(obj, settings) {
var result = [],
/*jshint latedef:false, laxbreak:true*/

function processKeyList(keyList) {
var key, nI, nL;
for (nI = 0, nL = keyList.length; nI < nL; nI++) {
key = keyList[nI];
if ((bAll || checkField(obj, key, filter, settings)) && ! (key in addedKeyMap)) {
result.push(key);
addedKeyMap[key] = null;
}
}
}

var addedKeyMap = {},
bAll = ! settings || ! ("filter" in settings),
getOwnPropertySymbols = Object.getOwnPropertySymbols,
bProcessSymbols = typeof getOwnPropertySymbols === "function",
filter = bAll ? null : settings.filter,
sKey;
for (sKey in obj) {
if (bAll || checkField(obj, sKey, filter, settings)) {
result.push(sKey);
bOwn = bAll ? false : filter === "own",
bNotOwn = bAll ? false : filter === "!own",
bUseFilter = bAll ? false : ! bOwn && ! bNotOwn,
result = [],
target = obj;
while (target) {
if (bAll || bUseFilter || (bOwn && target === obj) || (bNotOwn && target !== obj)) {
processKeyList(getOwnPropertyNames(target));
if (bProcessSymbols) {
processKeyList(getOwnPropertySymbols(target));
}
}
target = bAll || bNotOwn || bUseFilter
? getPrototypeOf(target)
: null;
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/adam.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b47e439

Please sign in to comment.