From 505e91e4b4a360297ec2e6db9a437891a50b6586 Mon Sep 17 00:00:00 2001 From: dpgraham Date: Mon, 10 Dec 2018 14:43:43 -0800 Subject: [PATCH] Add wrap element helper --- README.md | 2 ++ lib/util.js | 9 ++++++++- test/util-specs.js | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 78cf422f..296cb682 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ Also note that `fs.mkdir` doesn't throw an error if the directory already exists - util.localIp - util.cancellableDelay - util.multiResolve - multiple path.resolve +- util.unwrapElement - parse an element ID from an element object: e.g.: `{ELEMENT: 123, "element-6066-11e4-a52e-4f735466cecf": 123}` returns `123` +- util.wrapElement - convert an element ID to an element object of the form: e.g.: `123` returns `{ELEMENT: 123, "element-6066-11e4-a52e-4f735466cecf": 123}` - *fs.hasAccess* - use this over `fs.access` - *fs.exists* - calls `fs.hasAccess` diff --git a/lib/util.js b/lib/util.js index 1bc4b325..0433d51d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -123,6 +123,13 @@ function unwrapElement (el) { return el; } +function wrapElement (elementId) { + return { + ELEMENT: elementId, + [W3C_WEB_ELEMENT_IDENTIFIER]: elementId, + }; +} + /* * Returns object consisting of all properties in the original element * which were truthy given the predicate. @@ -219,7 +226,7 @@ async function isSameDestination (path1, path2, ...pathN) { export { hasValue, escapeSpace, escapeSpecialChars, localIp, cancellableDelay, - multiResolve, safeJsonParse, unwrapElement, filterObject, + multiResolve, safeJsonParse, wrapElement, unwrapElement, filterObject, toReadableSizeString, isSubPath, W3C_WEB_ELEMENT_IDENTIFIER, isSameDestination, }; diff --git a/test/util-specs.js b/test/util-specs.js index d708a113..a62067ba 100644 --- a/test/util-specs.js +++ b/test/util-specs.js @@ -245,6 +245,15 @@ describe('util', function () { }); }); + describe('wrapElement', function () { + it('should include ELEMENT and w3c element', function () { + util.wrapElement(123).should.eql({ + [util.W3C_WEB_ELEMENT_IDENTIFIER]: 123, + ELEMENT: 123, + }); + }); + }); + describe('toReadableSizeString', function () { it('should fail if cannot convert to Bytes', function () { (() => util.toReadableSizeString('asdasd')).should.throw(/Cannot convert/);