mirror of
https://github.com/systemjs/systemjs.git
synced 2026-02-01 15:59:54 +00:00
update iterator handling
This commit is contained in:
parent
26673d8201
commit
532fdbdded
14
docs/api.md
14
docs/api.md
@ -88,18 +88,14 @@ System.set('http://site.com/normalized/module/name.js', {
|
||||
|
||||
If `module` is an existing Module Namespace, it will be used by reference.
|
||||
|
||||
#### System.entries() -> Array
|
||||
#### System.entries() -> Iterator<[key, module]>
|
||||
Type: `Function`
|
||||
|
||||
Allows you to retrieve all modules in the System registry. Each value will be an array with two values: a key and the module. Also available
|
||||
at `System[Symbol.iterator]`.
|
||||
Allows you to retrieve all modules in the System registry. Each value will be an array with two values: a key and the module.
|
||||
|
||||
```js
|
||||
System.entries().forEach((key, value) => {
|
||||
console.log(entry); // ['http://localhost/path-to-file.js', {exportName: 'exportedValue'}]
|
||||
for (const [id, ns] of System.entries()) {
|
||||
console.log(id); // 'http://localhost/path-to-file.js'
|
||||
console.log(ns); // { exportName: 'value' }
|
||||
});
|
||||
|
||||
for (let entry of System) {
|
||||
console.log(entry); // ['http://localhost/path-to-file.js', {exportName: 'exportedValue'}]
|
||||
}
|
||||
```
|
||||
@ -60,23 +60,19 @@ systemJSPrototype.delete = function (id) {
|
||||
return delete this[REGISTRY][id];
|
||||
};
|
||||
|
||||
systemJSPrototype.entries = function () {
|
||||
const registry = this[REGISTRY];
|
||||
return Object.keys(registry).map(function (key) {
|
||||
return [key, registry[key].n];
|
||||
});
|
||||
}
|
||||
const iterator = typeof Symbol !== 'undefined' && Symbol.iterator;
|
||||
|
||||
if (typeof Symbol !== 'undefined') {
|
||||
systemJSPrototype[Symbol.iterator] = function () {
|
||||
const registry = this[REGISTRY];
|
||||
const keys = Object.keys(registry);
|
||||
let index = 0;
|
||||
return {
|
||||
next() {
|
||||
const key = keys[index++];
|
||||
return { done: index > keys.length, value: key && [key, registry[key].n] };
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
systemJSPrototype.entries = function () {
|
||||
const registry = this[REGISTRY], keys = Object.keys(registry);
|
||||
let index = 0;
|
||||
return {
|
||||
next () {
|
||||
const key = keys[index++];
|
||||
return {
|
||||
done: index > keys.length,
|
||||
value: key && [key, registry[key].n]
|
||||
};
|
||||
},
|
||||
[iterator]: function() { return this }
|
||||
};
|
||||
};
|
||||
|
||||
@ -143,7 +143,7 @@ describe('Core API', function () {
|
||||
await loader.import('h');
|
||||
|
||||
let foundH = false;
|
||||
for (let entry of loader) {
|
||||
for (let entry of loader.entries()) {
|
||||
if (entry[0] === 'h' && entry[1].a === 'b') {
|
||||
foundH = true;
|
||||
}
|
||||
@ -156,7 +156,7 @@ describe('Core API', function () {
|
||||
loader.set('i', {a: 'b'});
|
||||
await loader.import('i');
|
||||
|
||||
assert(loader.entries().some(entry => entry[0] === 'i' && entry[1].a === 'b'));
|
||||
assert([...loader.entries()].some(entry => entry[0] === 'i' && entry[1].a === 'b'));
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user