Skip to content

Commit

Permalink
Add path library.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaste committed Jul 23, 2016
1 parent a64f227 commit 0320763
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/lib/path.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<script>

Polymer.Path = {

head: function(path) {
var dotIndex = path.indexOf('.');
if (dotIndex === -1) {
return path;
}
return path.slice(0, dotIndex);
},

isDeep: function(path) {
return path.indexOf('.') !== -1;
},

// Given `base` is `foo.bar`, `foo` is an ancestor, `foo.bar` is not
isAncestor: function(base, path) {
// base.startsWith(path + '.');
return base.indexOf(path + '.') === 0;
},

// Given `base` is `foo.bar`, `foo.bar.baz` is an descendant
isDescendant: function(base, path) {
// path.startsWith(base + '.');
return path.indexOf(base + '.') === 0;
},

// can be read as: from to path
translate: function(base, newBase, path) {
// Defense?
return newBase + path.slice(base.length);
},

matches: function(base, wildcard, path) {
return (base === path) ||
this.isAncestor(base, path) ||
(Boolean(wildcard) && this.isDescendant(base, path));
}


};

</script>
1 change: 1 addition & 0 deletions test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
'unit/bind.html',
'unit/bind.html?dom=shadow',
'unit/notify-path.html',
'unit/path.html',
'unit/array-selector.html',
'unit/events.html',
'unit/gestures.html',
Expand Down
96 changes: 96 additions & 0 deletions test/unit/path.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<!doctype html>
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<meta charset="utf-8">
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<link rel="import" href="../../src/polymer-lib.html">
<link rel="import" href="../../src/lib/path.html">
</head>
<body>

<script>
suite('path utilities', function() {

var Path;

setup(function() {
Path = Polymer.Path;
});

test('head()', function() {
assert.equal(Path.head('foo'), 'foo');
assert.equal(Path.head('foo.bar'), 'foo');
});

test('isDeep()', function() {
assert.equal(Path.isDeep('foo'), false);
assert.equal(Path.isDeep('foo.bar'), true);
});

test('isAncestor()', function() {
assert.equal(Path.isAncestor('foo.bar', 'foo'), true);

assert.equal(Path.isAncestor('foo.bar', 'foo.bar'), false);
assert.equal(Path.isAncestor('foo.bar', 'foo.baz'), false);
assert.equal(Path.isAncestor('foo.bar', 'fooz'), false);
assert.equal(Path.isAncestor('foo.bar', 'bar'), false);
assert.equal(Path.isAncestor('foo.bar', 'foo.bars'), false);
assert.equal(Path.isAncestor('foo.bar', 'foo.bar.quux'), false);


assert.equal(Path.isAncestor('foo.bar.baz', 'foo'), true);
assert.equal(Path.isAncestor('foo.bar.baz', 'foo.bar'), true);

assert.equal(Path.isAncestor('foo.bar.baz', 'foo.baz'), false);
assert.equal(Path.isAncestor('foo.bar.baz', 'foo.bars'), false);
});

test('isDescendant()', function() {
assert.equal(Path.isDescendant('foo.bar', 'foo.bar.baz'), true);
assert.equal(Path.isDescendant('foo.bar', 'foo.bar'), false);
assert.equal(Path.isDescendant('foo.bar', 'foo.bars'), false);
assert.equal(Path.isDescendant('foo.bar', 'foo'), false);
assert.equal(Path.isDescendant('foo.bar', 'bar'), false);

assert.equal(Path.isDescendant('foo', 'foo.bar'), true);
assert.equal(Path.isDescendant('foo', 'foo'), false);
});

test('translate()', function() {
assert.equal(Path.translate('foo', 'baz', 'foo.bar'),
'baz.bar');
assert.equal(Path.translate('foo', 'quux', 'foo.bar.baz'),
'quux.bar.baz');
assert.equal(Path.translate('foo.bar', 'quux', 'foo.bar.baz'),
'quux.baz');
});

test('matches()', function() {
assert.equal(Path.matches('foo.bar', false, 'foo'), true);
assert.equal(Path.matches('foo.bar', false, 'foo.bar'), true);
assert.equal(Path.matches('foo.bar', false, 'fooz'), false);
assert.equal(Path.matches('foo.bar', false, 'foo.baz'), false);
assert.equal(Path.matches('foo.bar', false, 'foo.bars'), false);
assert.equal(Path.matches('foo.bar', false, 'foo.bar.baz'), false);


// deep wildcard matches
assert.equal(Path.matches('foo.bar', true, 'foo'), true);
assert.equal(Path.matches('foo.bar', true, 'foo.bar'), true);
assert.equal(Path.matches('foo.bar', true, 'foo.bar.baz'), true);
assert.equal(Path.matches('foo.bar', true, 'fooz'), false);
assert.equal(Path.matches('foo.bar', true, 'foo.baz'), false);
assert.equal(Path.matches('foo.bar', true, 'foo.bars'), false);
});
});
</script>

0 comments on commit 0320763

Please sign in to comment.