1.0.2 • Published 5 years ago

iterate-value v1.0.2

Weekly downloads
2,262,601
License
MIT
Repository
github
Last release
5 years ago

iterate-value Version Badge

Build Status dependency status dev dependency status License Downloads

npm badge

Iterate any iterable JS value. Works robustly in all environments, all versions.

In modern engines, [...value] or Array.from(value) or for (const item of value) { } are sufficient to iterate an iterable value (an object with a Symbol.iterator method). However, older engines:

  • may lack Symbol, array spread, or for..of support altogether
  • may have Symbol.iterator but not implement it on everything it should, like arguments objects
  • may have Map and Set, but a non-standard name for the iterator-producing method (.iterator or ['@@iterator'], eg) and no syntax to support it
  • may be old versions of Firefox that produce values until they throw a StopIteration exception, rather than having iteration result objects
  • may be polyfilled/shimmed/shammed, with es6-shim or core-js or similar

This library attempts to provide an abstraction over all that complexity!

If called with a single value, it will return an array of the yielded values. If also called with a callback function, it will instead call that callback once for each yielded value.

In node v13+, exports is used by the es-get-iterator dependency to provide a lean implementation that lacks all the complexity described above, in combination with the browser field so that bundlers will pick up the proper implementation.

If you are targeting browsers that definitely all have Symbol support, then you can configure your bundler to replace require('has-symbols')() with a literal true, which should allow dead code elimination to reduce the size of the bundled code.

Example

var iterate = require('iterate-value');
var assert = require('assert');

assert.deepEqual(iterate('a 💩'), ['a', ' ', '💩']);
assert.deepEqual(iterate([1, 2]), [1, 2]);
assert.deepEqual(iterate(new Set([1, 2])), [1, 2]);
assert.deepEqual(iterate(new Map([[1, 2], [3, 4]])), [[1, 2], [3, 4]]);

function assertWithCallback(iterable, expected) {
	var values = [];
	var callback = function (x) { values.push(x); };
	iterate(iterable, callback);
	assert.deepEqual(values, expected);
}
assertWithCallback('a 💩', ['a', ' ', '💩']);
assertWithCallback([1, 2], [1, 2]);
assertWithCallback(new Set([1, 2]), [1, 2]);
assertWithCallback(new Map([[1, 2], [3, 4]]), [[1, 2], [3, 4]]);

Tests

Simply clone the repo, npm install, and run npm test

promise.allsettledeasy-select-rnreact-native-printer-brothersreact-native-shekhar-bridge-test@oiti/documentoscopy-react-nativequoc-test@saaspe/componentsluminos-ui-core@everything-registry/sub-chunk-1940jawwy-sdkjawwy_gamification_releasereact-native-sphereuisphereuijawwy_libraryreact-native-credit-card-pkgreact-native-jawwy_samplegriffin-ui-libraryfn-lib-examplefmslfluent.adflow.reactnativesdkfluent.adflow.reactnativesdk-alphaframework_test_library_sixdeeframework_test_library_sixdee_newframework_test_library_sixdee_new_newgamification-integration-newkhaled-salem-custom-componentshong1-utilsparvan_componentsparvan_reactjs_componentsori-bot-react-nativejesh-calculationjordy-frijters-test-libjrennsoh88-react-native-scroll-indicatormap-tojsonnative-date-picker-modulenative-modal-damage-vehiclenew-awesome-4321@idas1/ui-component-lib@labiebhn_/react-native-multiplier@lego-js/tests@lehuyaa/my-assets@cs6/react-native-test-native-view-library@damruravihara/react-native-testing-package@jfilipe-sparta/react-native-module_2@kylesferrazza/graphql-relay-updated@kgit/readabilitydogandev-simple-toastdemo-test-scrndesign-system-fitbank-450cotrader-dex-sdkexpo-renavigatees-setpixiu-swap-corepixiuswap-libs-sdkpnm-yph-react-native-custom-componentsreact-form-component-libraryproject-wajs-dvpromise.anygoogle-remakereact-native-azure-communication-servicesreact-native-awesome-android-123react-native-awesome-android-123-zeotapjawwy_library_newjawy_library_v1gamification-jawwy-libraryframework_test_library_sixdee_new_jawwytest_lib_module_aarquickcapture_react_nativern-agora-ios-mreactnativelyreat-native-multiplierkprrn-circular-chartrn-counter-demorn-check-btnrn-currency-formatterrnttlockrn_unique_device_idrn-session-multiplier-demotest-haptik-libtest-library-123scout-chatbot-widgetsequelcomponentsmart_one_connectset-tojsonsvelte-component-lib@adatechnology/react-native-android-getnet-pos@adembacaj/react-native-google-paywifi_configuration_package@amiruldev/wajs@amirdiafi/react-native-ios-haptics@ali5049/react-native-buttons@_nomtek/react-native-shimmer-animation@aviinash_jha/react-native-test_multiply@blkmarketco/components-library@brantalikp/rn-resize@con-test/react-native-concent-common@d-fischer/promise.allsettledvantiq-reacttypescript-promise-anyu-library
1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago