Skip to content

clean iterator prototype to allow tree shaking #2126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2025
Merged

Conversation

jdeniau
Copy link
Member

@jdeniau jdeniau commented Jun 18, 2025

Using X.prototype is an old code style, and forbid tree-shaking of the library.

As the 6.x branch will migrate to a modern coding style, let's start with the simple Iterator class.

Testing tree-shakeability

Replace the Immutable.js file by this

/* eslint-disable import/order */
// import { Seq } from './Seq';
// import { OrderedMap } from './OrderedMap';
// import { List } from './List';
// import { Map } from './Map';
// import { Stack } from './Stack';
// import { OrderedSet } from './OrderedSet';
// import { PairSorting } from './PairSorting';
// import { Set } from './Set';
// import { Record } from './Record';
// import { Range } from './Range';
// import { Repeat } from './Repeat';
// import { is } from './is';
// import { fromJS } from './fromJS';

import isPlainObject from './utils/isPlainObj';

// Functional predicates
import { isImmutable } from './predicates/isImmutable';
import { isCollection } from './predicates/isCollection';
import { isKeyed } from './predicates/isKeyed';
import { isIndexed } from './predicates/isIndexed';
import { isAssociative } from './predicates/isAssociative';
import { isOrdered } from './predicates/isOrdered';
import { isValueObject } from './predicates/isValueObject';
import { isSeq } from './predicates/isSeq';
import { isList } from './predicates/isList';
import { isMap } from './predicates/isMap';
import { isOrderedMap } from './predicates/isOrderedMap';
import { isStack } from './predicates/isStack';
import { isSet } from './predicates/isSet';
import { isOrderedSet } from './predicates/isOrderedSet';
import { isRecord } from './predicates/isRecord';

// import { Collection } from './CollectionImpl';
import { hash } from './Hash';

// Functional read/write API
import { get } from './functional/get';
import { getIn } from './functional/getIn';
import { has } from './functional/has';
import { hasIn } from './functional/hasIn';
// import { merge, mergeDeep, mergeWith, mergeDeepWith } from './functional/merge';
// import { remove } from './functional/remove';
// import { removeIn } from './functional/removeIn';
// import { set } from './functional/set';
// import { setIn } from './functional/setIn';
// import { update } from './functional/update';
// import { updateIn } from './functional/updateIn';

import { version } from '../package.json';

// Note: Iterable is deprecated
// const Iterable = Collection;

export {
  // version,
  // Collection,
  // Iterable,
  // Seq,
  // Map,
  // OrderedMap,
  // List,
  // Stack,
  // Set,
  // OrderedSet,
  // PairSorting,
  // Record,
  // Range,
  // Repeat,
  // is,
  // fromJS,
  // hash,
  // isImmutable,
  // isCollection,
  // isKeyed,
  // isIndexed,
  // isAssociative,
  // isOrdered,
  // isPlainObject,
  // isValueObject,
  isSeq,
  // isList,
  // isMap,
  // isOrderedMap,
  // isStack,
  // isSet,
  // isOrderedSet,
  // isRecord,
  // get,
  // getIn,
  // has,
  // hasIn,
  // merge,
  // mergeDeep,
  // mergeWith,
  // mergeDeepWith,
  // remove,
  // removeIn,
  // set,
  // setIn,
  // update,
  // updateIn,
};

(for now the circular dependency makes it untestable if we import / export all)

Compiling this file with rollup

import {
  // is,
  // isList,
  // isStack,
  // isMap,
  // isOrderedMap,
  // isSet,
  // isOrderedSet,
  isSeq,
} from './dist/immutable.mjs';

console.log(isSeq({ a: 1, b: 2 })); // false

Will give the following output:

/**
 * @license
 * MIT License
 * 
 * Copyright (c) 2014-present, Lee Byron and other 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.
 */
const IS_SEQ_SYMBOL = '@@__IMMUTABLE_SEQ__@@';

/**
 * True if `maybeSeq` is a Seq.
 */
function isSeq(maybeSeq) {
  return Boolean(maybeSeq &&
  // @ts-expect-error: maybeSeq is typed as `{}`, need to change in 6.0 to `maybeSeq && typeof maybeSeq === 'object' && MAYBE_SEQ_SYMBOL in maybeSeq`
  maybeSeq[IS_SEQ_SYMBOL]);
}

// True if Object.defineProperty works as expected. IE8 fails this test.
// TODO remove this as widely available https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
(function () {
  try {
    Object.defineProperty({}, '@', {});
    return true;
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
  } catch (e) {
    return false;
  }
})();

console.log(isSeq({ a: 1, b: 2 })); // false

Previously, we add all the Iterator class in here!

@jdeniau jdeniau added this to the 6.0 milestone Jun 18, 2025
@jdeniau jdeniau force-pushed the clean-iterator-prototype branch from e8b6482 to caa4f9a Compare June 18, 2025 12:14
@jdeniau jdeniau force-pushed the clean-iterator-prototype branch from caa4f9a to 8725cf4 Compare June 18, 2025 12:19
@jdeniau jdeniau merged commit 9927ca4 into 6.x Jun 18, 2025
5 checks passed
@jdeniau jdeniau deleted the clean-iterator-prototype branch June 18, 2025 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant