123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- // Convert complex js object to dot notation js object
- // url: https://github.com/vardars/dotize
- // author: vardars
- var dotize = dotize || {};
- dotize.convert = function(obj, prefix) {
- var newObj = {};
- if ((!obj || typeof obj != "object") && !Array.isArray(obj)) {
- if (prefix) {
- newObj[prefix] = obj;
- return newObj;
- } else {
- return obj;
- }
- }
- function isNumber(f) {
- return !isNaN(parseInt(f));
- }
- function isEmptyObj(obj) {
- for (var prop in obj) {
- if (Object.hasOwnProperty.call(obj, prop))
- return false;
- }
- }
- function getFieldName(field, prefix, isRoot, isArrayItem, isArray) {
- if (isArray)
- return (prefix ? prefix : "") + (isNumber(field) ? "[" + field + "]" : (isRoot ? "" : ".") + field);
- else if (isArrayItem)
- return (prefix ? prefix : "") + "[" + field + "]";
- else
- return (prefix ? prefix + "." : "") + field;
- }
- return function recurse(o, p, isRoot) {
- var isArrayItem = Array.isArray(o);
- for (var f in o) {
- var currentProp = o[f];
- if (currentProp && typeof currentProp === "object") {
- if (Array.isArray(currentProp)) {
- newObj = recurse(currentProp, getFieldName(f, p, isRoot, false, true), isArrayItem); // array
- } else {
- if (isArrayItem && isEmptyObj(currentProp) == false) {
- newObj = recurse(currentProp, getFieldName(f, p, isRoot, true)); // array item object
- } else if (isEmptyObj(currentProp) == false) {
- newObj = recurse(currentProp, getFieldName(f, p, isRoot)); // object
- } else {
- //
- }
- }
- } else {
- if (isArrayItem || isNumber(f)) {
- newObj[getFieldName(f, p, isRoot, true)] = currentProp; // array item primitive
- } else {
- newObj[getFieldName(f, p, isRoot)] = currentProp; // primitive
- }
- }
- }
- return newObj;
- }(obj, prefix, true);
- };
- dotize.stringify = function(object) {
- var dotObj = dotize.convert(object);
-
- }
- if (typeof module != "undefined") {
- module.exports = dotize;
- }
|