4loop 中文文档教程
4loop
对所有不同的类型保持相同的 for 循环语法。 只需传递您的约束和回调,我们将确定最佳行动方案。
如果你传入任何带有值(obj、map、arr)的东西,你总是会首先返回它,否则它将是迭代器或键。 请看下面的例子
Install
$ npm install 4loop
代码
Use
const fourLoop = require('4loop');
const obj = { foo: 'bar', baz: 'cat' };
const arr = ['foo', 'bar', 'baz', 'cat'];
const arrLength = arr.length;
const mySet = new Set(['foo', 'bar', { baz: 'cats' }]);
const myMap = new Map([['foo', 'bar'], ['baz', 'cats'], ['dogs', undefined]]);
fourLoop(obj, (value, key, iterator) => {
// Do magic here
if (iterator === 1) {
console.log(value, key, iterator); // Prints cat, baz, 1
}
});
fourLoop(arr, (value, key, iterator) => {
// Do magic here
if (iterator === 1) {
console.log(value, key, iterator); // Prints bar, bar, 1
}
});
fourLoop(arrLength, (value, key, iterator) => {
// Do magic here
if (iterator === 1) {
console.log(value, key, iterator); // Prints 1, 1, 1
}
});
fourLoop(mySet, (value, key, iterator) => {
// Do magic here
if (iterator === 1) {
console.log(value, key, iterator); // Prints bar, bar, 1
}
});
fourLoop(myMap, (value, key, iterator) => {
// Do magic here
if (iterator === 1) {
console.log(value, key, iterator); // Prints cats, baz, 1
}
});
Developing and contributing to 4loop
Folder structure
主体在index.js
。
测试位于 test/spec
目录中。 请遵循 xxxx.spec.js
的命名约定
Running tests
我们使用 Feather-test 这是类似于用于单元测试的茉莉花。 除非有非常令人信服的理由使用不同的东西,否则请继续使用 Feather 进行测试。 现有测试位于 spec 文件夹中。 以下是一些有用的命令快捷方式:
Run all the tests:
npm test
Run only some tests:
npm test --run=nameOfFile
Before submitting a pull request
请确保所有代码都支持所有版本的节点。
我们使用 ESLint 来保持语法一致性,并且 linting 规则包含在此存储库中。 运行 npm test
也会检查 linting 规则。 在提交拉取请求之前,请确保您的代码没有 linting 错误。
<代码>eslint 。 --fix 还将自动修复任何 linting 错误。
License
4loop
Keep your for loop syntax the same for all different types. Just pass in your constraints and callback and we will determine best course of action.
If you pass in anything with a value(obj, map, arr), you will always get that returned first otherwise it will be the iterator or key. Please see examples below
Install
$ npm install 4loop
Use
const fourLoop = require('4loop');
const obj = { foo: 'bar', baz: 'cat' };
const arr = ['foo', 'bar', 'baz', 'cat'];
const arrLength = arr.length;
const mySet = new Set(['foo', 'bar', { baz: 'cats' }]);
const myMap = new Map([['foo', 'bar'], ['baz', 'cats'], ['dogs', undefined]]);
fourLoop(obj, (value, key, iterator) => {
// Do magic here
if (iterator === 1) {
console.log(value, key, iterator); // Prints cat, baz, 1
}
});
fourLoop(arr, (value, key, iterator) => {
// Do magic here
if (iterator === 1) {
console.log(value, key, iterator); // Prints bar, bar, 1
}
});
fourLoop(arrLength, (value, key, iterator) => {
// Do magic here
if (iterator === 1) {
console.log(value, key, iterator); // Prints 1, 1, 1
}
});
fourLoop(mySet, (value, key, iterator) => {
// Do magic here
if (iterator === 1) {
console.log(value, key, iterator); // Prints bar, bar, 1
}
});
fourLoop(myMap, (value, key, iterator) => {
// Do magic here
if (iterator === 1) {
console.log(value, key, iterator); // Prints cats, baz, 1
}
});
Developing and contributing to 4loop
Folder structure
The main body of code is in index.js
.
The tests are in the test/spec
directory. Please follow naming convention with xxxx.spec.js
Running tests
We use Feather-test which is similar to jasmine for unit tests. Unless there is a very compelling reason to use something different, please continue using Feather for tests. The existing tests are in the spec folder. Here are some useful command shortcuts:
Run all the tests:
npm test
Run only some tests:
npm test --run=nameOfFile
Before submitting a pull request
Please make sure all code supports all versions of node.
We use ESLint for syntax consistency, and the linting rules are included in this repository. Running npm test
will check the linting rules as well. Please make sure your code has no linting errors before submitting a pull request.
eslint . --fix
will also automatically fix any linting errors.