Skip to content
This repository has been archived by the owner on Aug 11, 2019. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed May 7, 2017
0 parents commit 2d45163
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 0 deletions.
43 changes: 43 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"root": true,
"rules": {
"indent": [ 2, "tab", { "SwitchCase": 1 } ],
"semi": [ 2, "always" ],
"keyword-spacing": [ 2, { "before": true, "after": true } ],
"space-before-blocks": [ 2, "always" ],
"no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
"no-cond-assign": 0,
"no-unused-vars": 2,
"object-shorthand": [ 2, "always" ],
"no-const-assign": 2,
"no-class-assign": 2,
"no-this-before-super": 2,
"no-var": 2,
"no-unreachable": 2,
"valid-typeof": 2,
"quote-props": [ 2, "as-needed" ],
"one-var": [ 2, "never" ],
"prefer-arrow-callback": 2,
"prefer-const": [ 2, { "destructuring": "all" } ],
"arrow-spacing": 2,
"no-inner-declarations": 0
},
"env": {
"es6": true,
"browser": true,
"node": true,
"mocha": true
},
"extends": [
"eslint:recommended",
"plugin:import/errors",
"plugin:import/warnings"
],
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"settings": {
"import/core-modules": [ "svelte" ]
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
dist
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2017 [these people](https://github.com/sveltejs/svelte-extras/graphs/contributors)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# svelte-extras

Extra methods for [Svelte](https://svelte.technology) components.

## Usage

Install with npm or yarn...

```bash
npm install --save svelte-extras
```

...then add to your component methods:

```html
<input bind:value='newTodo'>
<button on:click='push("todos", newTodo)'>add todo</button>

<ul>
{{#each todos as todo, i}}
<li>
<button on:click='splice("todos", i, 1)'>x</button>
{{todo}}
</li>
{{/each}}
</ul>

<script>
import { push, splice } from 'svelte-extras';
export default {
data: function () {
return {
todos: ['add some more todos']
};
},
methods: {
push,
splice
}
};
</script>
```

## Available methods

* push
* pop
* shift
* unshift
* splice
* sort
* reverse

These all work exactly as their `Array.prototype` counterparts, except that the first argument must be the *keypath* that points to the array. The following are all examples of keypaths:

```js
component.push('todos', 'finish writing this documentation');
component.push('foo.bar.baz', 42);
component.push('rows[4]', cell);
```

## Tree-shaking

If you're using a module bundler that supports tree-shaking, such as [Rollup](https://rollupjs.org), only the methods your components use will be included in your app.


## Universal module definition

If you *really* need it, a UMD build is available at [svelte-extras/dist/svelte-extras.umd.js](https://unpkg.com/svelte-extras/dist/svelte-extras.js), and will register itself as `svelte.extras`. We recommend using a module bundler instead, however.


## License

[MIT](LICENSE)
39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "svelte-extras",
"version": "1.0.0",
"description": "Extra methods for Svelte components",
"main": "dist/svelte-extras.cjs.js",
"module": "dist/svelte-extras.es.js",
"scripts": {
"build": "rollup -c",
"lint": "eslint src",
"prepublish": "npm run lint && npm test",
"test": "mocha",
"pretest": "npm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sveltejs/svelte-extras.git"
},
"keywords": [
"svelte"
],
"author": "Rich Harris",
"license": "MIT",
"bugs": {
"url": "https://github.com/sveltejs/svelte-extras/issues"
},
"homepage": "https://github.com/sveltejs/svelte-extras#readme",
"devDependencies": {
"@types/node": "^7.0.18",
"eslint": "^3.19.0",
"eslint-plugin-import": "^2.2.0",
"jsdom": "^10.1.0",
"mocha": "^3.3.0",
"rollup": "^0.41.6",
"rollup-plugin-buble": "^0.15.0",
"rollup-plugin-typescript": "^0.8.1",
"svelte": "^1.20.2",
"typescript": "^2.3.2"
}
}
14 changes: 14 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import typescript from 'rollup-plugin-typescript';

const pkg = require('./package.json');

export default {
entry: 'src/index.ts',
moduleName: 'svelte.extras',
plugins: [typescript({ typescript: require('typescript') })],
targets: [
{ dest: pkg.main, format: 'cjs' },
{ dest: pkg.module, format: 'es' },
{ dest: 'dist/svelte-extras.umd.js', format: 'umd' }
]
};
32 changes: 32 additions & 0 deletions src/array-methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const arrayNotationPattern = /\[\s*(\d+)\s*\]/g;

interface Component {
set(data: {}): void
get(key?: string): any
}

function makeArrayMethod(name: string) {
return function(this:Component, keypath: string, ...args: any[]) {
const parts = keypath.replace(arrayNotationPattern, '.$1').split('.');

const key = parts.shift();
const value = this.get(key);

let array = value;
while (parts.length)
array = array[parts.shift()];

const result = array[name](...args);
this.set({ [key]: value });

return result;
};
}

export const push = makeArrayMethod( 'push' );
export const pop = makeArrayMethod( 'pop' );
export const shift = makeArrayMethod( 'shift' );
export const unshift = makeArrayMethod( 'unshift' );
export const splice = makeArrayMethod( 'splice' );
export const sort = makeArrayMethod( 'sort' );
export const reverse = makeArrayMethod( 'reverse' );
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './array-methods.js';
35 changes: 35 additions & 0 deletions test/getComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { JSDOM } = require('jsdom');

const svelte = require('svelte');
const extras = require('../dist/svelte-extras.cjs.js');

module.exports = function getComponent(
template = `
{{#each array as item}}
({{item}})
{{/each}}
`,
data = { array: ['foo', 'bar', 'baz'] }
) {
const { window } = new JSDOM('<main></main>');
global.document = window.document;
const target = window.document.querySelector('main');

Object.assign(global, extras);

const Component = svelte.create(`
${template}
<script>
export default {
methods: { push, pop, shift, unshift, splice, sort, reverse }
}
</script>
`);
const component = new Component({
target,
data
});

return { component, target };
};
66 changes: 66 additions & 0 deletions test/htmlEqual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const assert = require('assert');
const { JSDOM } = require('jsdom');

function cleanChildren(node) {
let previous = null;

[...node.childNodes].forEach(child => {
if (child.nodeType === 8) {
// comment
node.removeChild(child);
return;
}

if (child.nodeType === 3) {
if (
node.namespaceURI === 'http://www.w3.org/2000/svg' &&
node.tagName !== 'text' &&
node.tagName !== 'tspan'
) {
node.removeChild(child);
}

child.data = child.data.replace(/\s{2,}/, '\n');

// text
if (previous && previous.nodeType === 3) {
previous.data += child.data;
previous.data = previous.data.replace(/\s{2,}/, '\n');

node.removeChild(child);
child = previous;
}
} else {
cleanChildren(child);
}

previous = child;
});

// collapse whitespace
if (node.firstChild && node.firstChild.nodeType === 3) {
node.firstChild.data = node.firstChild.data.replace(/^\s+/, '');
if (!node.firstChild.data) node.removeChild(node.firstChild);
}

if (node.lastChild && node.lastChild.nodeType === 3) {
node.lastChild.data = node.lastChild.data.replace(/\s+$/, '');
if (!node.lastChild.data) node.removeChild(node.lastChild);
}
}

const { window } = new JSDOM('');

assert.htmlEqual = (actual, expected, message) => {
window.document.body.innerHTML = actual.replace(/>[\s\r\n]+</g, '><').trim();
cleanChildren(window.document.body, '');
actual = window.document.body.innerHTML;

window.document.body.innerHTML = expected
.replace(/>[\s\r\n]+</g, '><')
.trim();
cleanChildren(window.document.body, '');
expected = window.document.body.innerHTML;

assert.deepEqual(actual, expected, message);
};
Loading

0 comments on commit 2d45163

Please sign in to comment.