Skip to content

Commit

Permalink
feat: rewrite in TS, start using named exports
Browse files Browse the repository at this point in the history
BREAKING CHANGE: previously: "import fillMissing from ..." - now "import { fillMissing } from ..."
  • Loading branch information
revelt committed Jan 3, 2021
1 parent 66b7407 commit 474db39
Show file tree
Hide file tree
Showing 21 changed files with 6,313 additions and 6,140 deletions.
3 changes: 2 additions & 1 deletion packages/object-fill-missing-keys/.npmignore
@@ -1,4 +1,4 @@
# .... generated using www.npmjs.com/package/lect ....
# generated using codsen.com/os/lect
#
#
# __ ______ ______ ______
Expand All @@ -23,3 +23,4 @@ test
.prettierignore
rollup.config.js
testStats.md
tsconfig.json
4 changes: 2 additions & 2 deletions packages/object-fill-missing-keys/README.md
Expand Up @@ -34,7 +34,7 @@ npm i object-fill-missing-keys

```js
import { strict as assert } from "assert";
import fillMissing from "object-fill-missing-keys";
import { fillMissing } from "object-fill-missing-keys";

// deleting key 'c', with value 'd'
assert.deepEqual(
Expand Down Expand Up @@ -67,6 +67,6 @@ Please [visit codsen.com](https://codsen.com/os/object-fill-missing-keys/) for a

MIT License

Copyright (c) 2010-2020 Roy Revelt and other contributors
Copyright (c) 2010-2021 Roy Revelt and other contributors

<img src="https://codsen.com/images/png-codsen-ok.png" width="98" alt="ok" align="center"> <img src="https://codsen.com/images/png-codsen-1.png" width="148" alt="codsen" align="center"> <img src="https://codsen.com/images/png-codsen-star-small.png" width="32" alt="star" align="center">
@@ -1 +1 @@
{"total":{"lines":{"total":52,"covered":52,"skipped":0,"pct":100},"statements":{"total":53,"covered":53,"skipped":0,"pct":100},"functions":{"total":7,"covered":7,"skipped":0,"pct":100},"branches":{"total":63,"covered":57,"skipped":0,"pct":90.48}}}
{"total":{"lines":{"total":51,"covered":51,"skipped":0,"pct":100},"statements":{"total":52,"covered":52,"skipped":0,"pct":100},"functions":{"total":7,"covered":7,"skipped":0,"pct":100},"branches":{"total":62,"covered":56,"skipped":0,"pct":90.32}}}
171 changes: 74 additions & 97 deletions packages/object-fill-missing-keys/dist/object-fill-missing-keys.cjs.js
Expand Up @@ -9,176 +9,153 @@

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
var clone = require('lodash.clonedeep');
var merge = require('object-merge-advanced');
var objectMergeAdvanced = require('object-merge-advanced');
var arrayiffyIfString = require('arrayiffy-if-string');
var allValuesEqualTo = require('object-all-values-equal-to');
var objectAllValuesEqualTo = require('object-all-values-equal-to');
var isObj = require('lodash.isplainobject');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);
var clone__default = /*#__PURE__*/_interopDefaultLegacy(clone);
var merge__default = /*#__PURE__*/_interopDefaultLegacy(merge);
var arrayiffyIfString__default = /*#__PURE__*/_interopDefaultLegacy(arrayiffyIfString);
var allValuesEqualTo__default = /*#__PURE__*/_interopDefaultLegacy(allValuesEqualTo);
var isObj__default = /*#__PURE__*/_interopDefaultLegacy(isObj);

function _typeof(obj) {
"@babel/helpers - typeof";

if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}

return _typeof(obj);
}

function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}

return obj;
}

function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);

if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}

return keys;
}

function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};

if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
var version = "7.11.1";

return target;
}
var defaults = {
placeholder: false,
doNotFillThesePathsIfTheyContainPlaceholders: [],
useNullAsExplicitFalse: true
}; // ===================================
// F ( )

function typ(something) {
if (isObj__default['default'](something)) {
return "plain object";
}

if (Array.isArray(something)) {
return "array";
}
return _typeof(something);

return typeof something;
}

function isStr(something) {
return typeof something === "string";
}

function existy(x) {
return x != null;
}
function fillMissingKeys(incompleteOriginal, schema, opts) {
var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "";
} // this function does the job, but it is not exposed because its first argument
// requirements are loose - it can be anything since it will be calling itself recursively
// with potentially AST contents (objects containing arrays containing objects etc.)


function fillMissingKeys(incompleteOriginal, schema, opts, path) {
if (path === void 0) {
path = "";
}

var incomplete = clone__default['default'](incompleteOriginal);
if (existy(incomplete) || !(path.length && opts.doNotFillThesePathsIfTheyContainPlaceholders.includes(path) && allValuesEqualTo__default['default'](incomplete, opts.placeholder))) {

if (existy(incomplete) || !(path.length && opts.doNotFillThesePathsIfTheyContainPlaceholders.includes(path) && objectAllValuesEqualTo.allEq(incomplete, opts.placeholder))) {
if (isObj__default['default'](schema) && isObj__default['default'](incomplete)) {
// traverse the keys on schema and add them onto incomplete
Object.keys(schema).forEach(function (key) {
var currentPath = "".concat(path ? "".concat(path, ".") : "").concat(key);
// calculate the path for current key
var currentPath = "" + (path ? path + "." : "") + key;

if (opts.doNotFillThesePathsIfTheyContainPlaceholders.includes(currentPath)) {
if (existy(incomplete[key])) {
if (allValuesEqualTo__default['default'](incomplete[key], opts.placeholder)) {
if (objectAllValuesEqualTo.allEq(incomplete[key], opts.placeholder)) {
incomplete[key] = opts.placeholder;
}
} else {
// just create the key and set to placeholder value
incomplete[key] = opts.placeholder;
}
}
if (!existy(incomplete[key]) || !(opts.doNotFillThesePathsIfTheyContainPlaceholders.includes(currentPath) && allValuesEqualTo__default['default'](incomplete[key], opts.placeholder))) {

if (!existy(incomplete[key]) || !(opts.doNotFillThesePathsIfTheyContainPlaceholders.includes(currentPath) && objectAllValuesEqualTo.allEq(incomplete[key], opts.placeholder))) {
incomplete[key] = fillMissingKeys(incomplete[key], schema[key], opts, currentPath);
}
});
} else if (Array.isArray(schema) && Array.isArray(incomplete)) {
if (incomplete.length === 0) {
return schema;
}

if (schema.length > 0) {
for (var i = incomplete.length; i--;) {
var currentPath = "".concat(path ? "".concat(path, ".") : "", "0");
var currentPath = (path ? path + "." : "") + "0";

if (isObj__default['default'](schema[0]) || Array.isArray(schema[0])) {
incomplete[i] = fillMissingKeys(incomplete[i], schema[0], opts, currentPath);
}
}
}
} else {
return merge__default['default'](schema, incomplete, {
return objectMergeAdvanced.mergeAdvanced(schema, incomplete, {
useNullAsExplicitFalse: opts.useNullAsExplicitFalse
});
}
}

return incomplete;
}
function fillMissingKeysWrapper(originalIncompleteWrapper, originalSchemaWrapper, originalOptsWrapper) {
} // =================================================
// T H E E X P O S E D F U N C T I O N


function fillMissing(originalIncompleteWrapper, originalSchemaWrapper, originalOptsWrapper) {
// first argument must be an object. However, we're going to call recursively,
// so we have to wrap the main function with another, wrapper-one, and perform
// object-checks only on that wrapper. This way, only objects can come in,
// but inside there can be whatever data structures.
//
// Also, wrapper function will shield the fourth argument from the outside API
//
if (arguments.length === 0) {
throw new Error("object-fill-missing-keys: [THROW_ID_01] All arguments are missing!");
}

if (!isObj__default['default'](originalIncompleteWrapper)) {
throw new Error("object-fill-missing-keys: [THROW_ID_02] First argument, input object must be a plain object. Currently it's type is \"".concat(typ(originalIncompleteWrapper), "\" and it's equal to: ").concat(JSON.stringify(originalIncompleteWrapper, null, 4)));
throw new Error("object-fill-missing-keys: [THROW_ID_02] First argument, input object must be a plain object. Currently it's type is \"" + typ(originalIncompleteWrapper) + "\" and it's equal to: " + JSON.stringify(originalIncompleteWrapper, null, 4));
}

if (!isObj__default['default'](originalSchemaWrapper)) {
throw new Error("object-fill-missing-keys: [THROW_ID_03] Second argument, schema object, must be a plain object. Currently it's type is \"".concat(typ(originalSchemaWrapper), "\" and it's equal to: ").concat(JSON.stringify(originalSchemaWrapper, null, 4)));
throw new Error("object-fill-missing-keys: [THROW_ID_03] Second argument, schema object, must be a plain object. Currently it's type is \"" + typ(originalSchemaWrapper) + "\" and it's equal to: " + JSON.stringify(originalSchemaWrapper, null, 4));
}
if (originalOptsWrapper !== undefined && originalOptsWrapper !== null && !isObj__default['default'](originalOptsWrapper)) {
throw new Error("object-fill-missing-keys: [THROW_ID_04] Third argument, schema object, must be a plain object. Currently it's type is \"".concat(typ(originalOptsWrapper), "\" and it's equal to: ").concat(JSON.stringify(originalOptsWrapper, null, 4)));
}
if (originalOptsWrapper === null) {
originalOptsWrapper = {};
}
var defaults = {
placeholder: false,
doNotFillThesePathsIfTheyContainPlaceholders: [],
useNullAsExplicitFalse: true
};
var opts = _objectSpread2(_objectSpread2({}, defaults), originalOptsWrapper);
opts.doNotFillThesePathsIfTheyContainPlaceholders = arrayiffyIfString__default['default'](opts.doNotFillThesePathsIfTheyContainPlaceholders);

if (originalOptsWrapper && !isObj__default['default'](originalOptsWrapper)) {
throw new Error("object-fill-missing-keys: [THROW_ID_04] Third argument, schema object, must be a plain object. Currently it's type is \"" + typ(originalOptsWrapper) + "\" and it's equal to: " + JSON.stringify(originalOptsWrapper, null, 4));
} // fill any settings with defaults if missing:


var opts = _objectSpread__default['default'](_objectSpread__default['default']({}, defaults), originalOptsWrapper || {});

opts.doNotFillThesePathsIfTheyContainPlaceholders = arrayiffyIfString.arrayiffy(opts.doNotFillThesePathsIfTheyContainPlaceholders);
var culpritsVal = null;
var culpritsIndex = null;

if (opts.doNotFillThesePathsIfTheyContainPlaceholders.length > 0 && !opts.doNotFillThesePathsIfTheyContainPlaceholders.every(function (key, idx) {
if (!isStr(key)) {
culpritsVal = key;
culpritsIndex = idx;
return false;
}

return true;
})) {
throw new Error("object-fill-missing-keys: [THROW_ID_06] opts.doNotFillThesePathsIfTheyContainPlaceholders element with an index number \"".concat(culpritsIndex, "\" is not a string! It's ").concat(typ(culpritsVal), ", equal to:\n").concat(JSON.stringify(culpritsVal, null, 4)));
throw new Error("object-fill-missing-keys: [THROW_ID_06] opts.doNotFillThesePathsIfTheyContainPlaceholders element with an index number \"" + culpritsIndex + "\" is not a string! It's " + typ(culpritsVal) + ", equal to:\n" + JSON.stringify(culpritsVal, null, 4));
}

return fillMissingKeys(clone__default['default'](originalIncompleteWrapper), clone__default['default'](originalSchemaWrapper), opts);
}

module.exports = fillMissingKeysWrapper;
exports.fillMissing = fillMissing;
exports.version = version;

0 comments on commit 474db39

Please sign in to comment.