Giter Site home page Giter Site logo

symbola / symbola Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 0.0 393.85 MB

Native JavaScript prototype extension with symbol protocols

Home Page: https://symbola.github.io/symbola/

License: ISC License

JavaScript 4.56% TypeScript 95.06% Shell 0.38%
protocol-oriented-programming symbol-protocols javascript protocol-oriented rust-traits typeclasses typescript cpp-concepts swift-protocols ad-hoc-polymorphism

symbola's Introduction

Symbola

GitHub workflow status Conventional Commits: 1.0.0 code style

Symbola is a proof-of-concept library for 🀯 extending the native JavaScript prototypes 🀯 with methods identified by symbols to prevent naming collisions, which enables method chaining syntax without having to wrap or convert the target. For example, Map and Set objects don't have built-in iteration methods, so iterating over them requires a conversion to an array:

const xs = new Set([1, 2, 3, 4])
// Converting to array with ... spread syntax to chain map and filter
const ys = [...xs].map((x) => x + 1).filter((x) => x % 2)

In comparison, extending the native prototypes allows to use iteration methods on iterables without conversion:

// Importing the unique symbols used to access iteration methods
import { map, filter } from '@symbola/iterable'

const xs = new Set([1, 2, 3, 4])
// Calling [map] and [filter] methods without conversion
const ys = xs[map]((x) => x + 1)[filter]((x) => x % 2)

⭐ Try Symbola in an interactive sandbox
πŸ“– Article exploring symbol protocol extensions in more depth

Rationale for symbol protocol extensions

The key difference when extending native prototypes with symbols as keys is that symbol primitives are unique, so there is no practical chance of repeating situations like SmooshGate, where a non-standard Array.prototype.flatten method prevented a standard method with the same name from being added. The trade off is that accessing methods with symbols requires the [] operator instead of the ., but it's a small difference compared to overcoming the limitations of method chaining that are well documented in, for example, the pipeline operator proposal. In short, extending native prototoypes is a workaround for making fluent interfaces more widely applicable without the downsides of other workarounds like temporary variables or wrappers like _.chain.

Similar features in other languages

A number of other languages have ad-hoc extension features like trait composition or protocol extensions, which allow to retroactively extend types without modifying their definition based on conformity to a specific blueprint, contract, convention or protocol. For example, JavaScript defines iteration protocols, which are a set of requirements for types to be iterable, which means returning an associated iterator type when the [Symbol.iterator]() method is called, which in turn implements methods to return iteration results. Conforming to the iteration protocols means that the type is supported by for-of loop syntax or yield or yield* operators in generator functions, and is also supported by third-party utility libraries like IxJS or iterall, but these libraries have generally had limited adoption because of the limitations of chaining syntax in JavaScript, where retroactively extending types with chainable methods requires using wrappers, or to extend the native prototypes as in the iteration helpers proposal, which means going through an exceedingly long standardization process.

Symbol protocol extensions are a userland workaround for the typical limitations of avoiding a nested coding style in JavaScript, and it uses the language's flexibility to implement something similar to Rust traits, Swift protocols or Haskell typeclasses, and an additional advantage over, for example, the proposed standard extensions to native prototypes is that symbol protocols work for all objects that implement them, not just those inheriting from specific intrinsics like %Iterator.prototype%.

Type safety

TypeScript allows constraining methods to just receivers that conform to a specific protocol, so symbol protocol methods are typed to prevent being used with non-conforming types like so:

{
  [filter]()<A>(this: Iterable<A>, fn: (a: A) => boolean) { ... }
}

The actual implementation for the symbol protocols also does not rely on anything magical and just add the methods to the native prototypes and use declaration merging to make TypeScript recognize the added methods.

Full implementation for the filterable protocol
import { extend } from '@symbola/core'

export const filter = Symbol('filter')

export default abstract class Filterable {
  *[filter]<T>(this: Iterable<T>, callback: (value: T) => boolean) {
    for (const value of this) {
      if (callback(value)) {
        yield value
      }
    }
  }
}

declare global {
  interface Object extends Filterable {}
}

extend(Object.prototype, Filterable.prototype)

Standard ad-hoc extension proposal

Aside from the |> pipeline operator proposal, there is also the :: bind operator proposal that addresses the same issue of ad-hoc extension of existing types, and there is also a stage-1 extensions proposal based on the same operator, which would essentially do the same as is currently already possible in userland with symbol protocol extensions.

Example use cases

Lazy iteration

The symbol methods are added to Object.prototype, so they work on any object that implements the JavaScript iteration protocols, including generators:

import { filter } from '@symbola/iterable'

function* naturals() {
  let i = 1
  while (true) {
    yield i++
  }
}
const xs = naturals()[filter]((x) => x % 2)

The above example doesn't get stuck in an infinite loop because it takes advantage of the laziness of iterators, which is not possible when converting iterators to arrays, because the array iteration methods are eager.

The @symbola/iterable package implements the methods from the iterator helpers proposal meant to support lazy iteration.

Logging

[log]() calls can be inserted in the code without having to wrap the target:

import { log } from '@symbola/core'

const foo = bar[log]().baz().qux()

Set methods

The @symbola/set package implements the proposed method extensions from the Set methods proposal.

Example
import { difference } from '@symbola/set'

const a = new Set([1, 2, 3, 4])
const b = new Set([3, 4, 5, 6])

a[difference](b) // -> Set([3, 4])

Usage

The library is divided into separate packages:

  • @symbola/core
  • @symbola/iterable
  • @symbola/async-iterable
  • @symbola/object
  • @symbola/function
  • @symbola/number
  • @symbola/set

Importing the specific package will extend the corresponding native prototypes.

Caveats

JavaScript symbols provide only weak encapsulation or privacy, so it's possible for code that uses Reflect.ownKeys on native prototypes to conflict with the custom symbol properties, but the practical chances of it are exceedingly low.

The symbol properties also won't work on objects that don't inherit from Object.prototype, like those created with Object.create(null) or null or undefined values.

Alternatives

symbola's People

Contributors

renovate[bot] avatar slikts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

symbola's Issues

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two Factor Authentication for your account, set its level to "Authorization only" in your account settings. semantic-release cannot publish with the default "
Authorization and writes" level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two Factor Authentication for your account, set its level to "Authorization only" in your account settings. semantic-release cannot publish with the default "
Authorization and writes" level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • fix(deps): update dependency fp-ts to v2.16.5
  • fix(deps): update dependency queueable to v5.3.2
  • chore(deps): update dependency esbuild to ^0.20.0
  • chore(deps): update dependency eslint-config-prettier to v9.1.0
  • chore(deps): update dependency eslint-plugin-jest to v27.9.0
  • chore(deps): update dependency knip to v2.43.0
  • chore(deps): update dependency prettier to v3.2.5
  • chore(deps): update dependency typescript to v5.4.5
  • chore(deps): update node.js to v18.20.2
  • chore(deps): update typescript-eslint monorepo to v6.21.0 (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)
  • chore(deps): update actions/setup-node action to v4
  • chore(deps): update codecov/codecov-action action to v4
  • chore(deps): update commitlint monorepo to v19 (major) (@commitlint/cli, @commitlint/config-conventional)
  • chore(deps): update dependency @types/prettier to v3
  • chore(deps): update dependency eslint to v9
  • chore(deps): update dependency eslint-plugin-jest to v28
  • chore(deps): update dependency husky to v9
  • chore(deps): update dependency knip to v5
  • chore(deps): update dependency lint-staged to v15
  • chore(deps): update dependency node to v20
  • chore(deps): update peaceiris/actions-gh-pages action to v4
  • chore(deps): update semantic-release monorepo (major) (@semantic-release/commit-analyzer, @semantic-release/github, @semantic-release/npm, @semantic-release/release-notes-generator, semantic-release)
  • chore(deps): update typescript-eslint monorepo to v7 (major) (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)
  • chore(deps): update yarn to v4
  • πŸ” Create all rate-limited PRs at once πŸ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/actions/setup/action.yml
  • actions/setup-node v3
.github/workflows/cicd.yml
  • actions/checkout v3
  • codecov/codecov-action v3
  • actions/checkout v3
  • actions/checkout v3
  • actions/checkout v3
  • peaceiris/actions-gh-pages v3
npm
.yarn/sdks/eslint/package.json
.yarn/sdks/prettier/package.json
.yarn/sdks/typescript/package.json
configs/babel/package.json
  • babel-plugin-transform-async-to-promises ^0.8.18
  • @types/babel__preset-env ^7.9.2
  • @types/eslint ^8.44.0
  • @types/prettier ^2.7.3
  • @typescript-eslint/eslint-plugin ^6.1.0
  • @typescript-eslint/parser ^6.1.0
  • eslint ^8.45.0
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-jest ^27.2.3
  • eslint-plugin-tsdoc ^0.2.17
  • prettier ^3.0.0
  • @babel/core ^7
  • @babel/preset-env ^7
  • @babel/preset-typescript ^7
  • node >= 18
  • node 18.17.1
configs/config/package.json
  • @babel/core ^7.22.9
  • @babel/preset-env ^7.22.9
  • @babel/preset-typescript ^7.22.5
  • @types/babel__core ^7.20.1
  • @types/babel__preset-env ^7.9.2
  • @types/eslint ^8.44.0
  • @types/prettier ^2.7.3
  • @typescript-eslint/eslint-plugin ^6.1.0
  • @typescript-eslint/parser ^6.1.0
  • eslint ^8.45.0
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-jest ^27.2.3
  • eslint-plugin-jest-formatting ^3.1.0
  • eslint-plugin-tsdoc ^0.2.17
  • prettier ^3.0.0
  • node >= 18
  • node 18.17.1
configs/eslint/package.json
  • @types/eslint ^8.44.0
  • @types/prettier ^2.7.3
  • @typescript-eslint/eslint-plugin ^6.1.0
  • @typescript-eslint/parser ^6.1.0
  • eslint ^8.45.0
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-jest ^27.2.3
  • eslint-plugin-jest-formatting ^3.1.0
  • eslint-plugin-tsdoc ^0.2.17
  • prettier ^3.0.0
  • @typescript-eslint/eslint-plugin *
  • @typescript-eslint/parser *
  • eslint *
  • eslint-config-prettier *
  • eslint-plugin-jest *
  • eslint-plugin-jest-formatting *
  • eslint-plugin-tsdoc *
  • node >= 18
  • node 18.17.1
configs/jest/package.json
  • node >= 18
  • node 18.17.1
configs/prettier/package.json
  • prettier *
  • node >= 18
  • node 18.17.1
configs/typescript/package.json
  • @types/jest ^29.5.3
  • node >= 18
  • node 18.17.1
package.json
  • @commitlint/cli ^17.6.6
  • @commitlint/config-conventional ^17.6.6
  • @dmeents/semantic-release-yarn ^1.1.11
  • @semantic-release/commit-analyzer ^10.0.1
  • @semantic-release/github ^9.0.4
  • @semantic-release/npm ^10.0.4
  • @semantic-release/release-notes-generator ^11.0.4
  • @types/eslint ^8.44.0
  • @types/node ^20.4.2
  • @types/prettier ^2.7.3
  • @types/semantic-release ^20.0.1
  • @typescript-eslint/eslint-plugin ^6.1.0
  • @typescript-eslint/parser ^6.1.0
  • babel-plugin-transform-async-to-promises ^0.8.18
  • eslint ^8.45.0
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-jest ^27.2.3
  • eslint-plugin-jest-formatting ^3.1.0
  • eslint-plugin-tsdoc ^0.2.17
  • husky ^8.0.3
  • knip ^2.16.0
  • lint-staged ^14.0.0
  • microbundle ^0.15.1
  • multi-semantic-release ^3.0.2
  • prettier ^3.0.0
  • rimraf ^5.0.1
  • semantic-release ^21.0.7
  • typedoc ^0.25.0
  • typescript ^5.1.6
  • node >= 18
  • node 19.9.0
  • yarn 3.6.3
packages/async-iterable/package.json
  • @babel/core ^7.22.9
  • @babel/preset-env ^7.22.9
  • @babel/preset-typescript ^7.22.5
  • @types/babel__core ^7.20.1
  • @types/babel__preset-env ^7.9.2
  • @types/eslint ^8.44.0
  • @types/jest ^29.5.3
  • @types/node ^20.4.2
  • @types/prettier ^2.7.3
  • @typescript-eslint/eslint-plugin ^6.1.0
  • @typescript-eslint/parser ^6.1.0
  • esbuild ^0.19.0
  • esbuild-jest ^0.5.0
  • eslint ^8.45.0
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-jest ^27.2.3
  • eslint-plugin-jest-formatting ^3.1.0
  • eslint-plugin-tsdoc ^0.2.17
  • jest ^29.6.1
  • jest-github-reporter ^1.1.1
  • microbundle ^0.15.1
  • prettier ^3.0.0
  • node >= 18
  • node 18.17.1
packages/core/package.json
  • @babel/core ^7.22.9
  • @babel/preset-env ^7.22.9
  • @babel/preset-typescript ^7.22.5
  • @types/babel__core ^7.20.1
  • @types/babel__preset-env ^7.9.2
  • @types/eslint ^8.44.0
  • @types/jest ^29.5.3
  • @types/node ^20.4.2
  • @types/prettier ^2.7.3
  • @typescript-eslint/eslint-plugin ^6.1.0
  • @typescript-eslint/parser ^6.1.0
  • esbuild ^0.19.0
  • esbuild-jest ^0.5.0
  • eslint ^8.45.0
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-jest ^27.2.3
  • eslint-plugin-jest-formatting ^3.1.0
  • eslint-plugin-tsdoc ^0.2.17
  • jest ^29.6.1
  • jest-github-reporter ^1.1.1
  • microbundle ^0.15.1
  • prettier ^3.0.0
  • node >= 18
  • node 18.17.1
packages/function/package.json
  • fp-ts ^2.16.0
  • tuplerone ^3.3.3
  • @babel/core ^7.22.9
  • @babel/preset-env ^7.22.9
  • @babel/preset-typescript ^7.22.5
  • @types/babel__core ^7.20.1
  • @types/babel__preset-env ^7.9.2
  • @types/eslint ^8.44.0
  • @types/jest ^29.5.3
  • @types/node ^20.4.2
  • @types/prettier ^2.7.3
  • @typescript-eslint/eslint-plugin ^6.1.0
  • @typescript-eslint/parser ^6.1.0
  • esbuild ^0.19.0
  • esbuild-jest ^0.5.0
  • eslint ^8.45.0
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-jest ^27.2.3
  • eslint-plugin-jest-formatting ^3.1.0
  • eslint-plugin-tsdoc ^0.2.17
  • jest ^29.6.1
  • jest-github-reporter ^1.1.1
  • microbundle ^0.15.1
  • prettier ^3.0.0
  • node >= 18
  • node 19.9.0
packages/iterable/package.json
  • queueable ^5.3.1
  • @babel/core ^7.22.9
  • @babel/preset-env ^7.22.9
  • @babel/preset-typescript ^7.22.5
  • @types/babel__core ^7.20.1
  • @types/babel__preset-env ^7.9.2
  • @types/eslint ^8.44.0
  • @types/jest ^29.5.3
  • @types/node ^20.4.2
  • @types/prettier ^2.7.3
  • @typescript-eslint/eslint-plugin ^6.1.0
  • @typescript-eslint/parser ^6.1.0
  • esbuild ^0.19.0
  • esbuild-jest ^0.5.0
  • eslint ^8.45.0
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-jest ^27.2.3
  • eslint-plugin-jest-formatting ^3.1.0
  • eslint-plugin-tsdoc ^0.2.17
  • jest ^29.6.1
  • jest-github-reporter ^1.1.1
  • microbundle ^0.15.1
  • prettier ^3.0.0
  • node >= 18
  • node 18.17.1
packages/number/package.json
  • @babel/core ^7.22.9
  • @babel/preset-env ^7.22.9
  • @babel/preset-typescript ^7.22.5
  • @types/babel__core ^7.20.1
  • @types/babel__preset-env ^7.9.2
  • @types/eslint ^8.44.0
  • @types/jest ^29.5.3
  • @types/node ^20.4.2
  • @types/prettier ^2.7.3
  • @typescript-eslint/eslint-plugin ^6.1.0
  • @typescript-eslint/parser ^6.1.0
  • esbuild ^0.19.0
  • esbuild-jest ^0.5.0
  • eslint ^8.45.0
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-jest ^27.2.3
  • eslint-plugin-jest-formatting ^3.1.0
  • eslint-plugin-tsdoc ^0.2.17
  • jest ^29.6.1
  • jest-github-reporter ^1.1.1
  • microbundle ^0.15.1
  • prettier ^3.0.0
  • node >= 18
  • node 18.17.1
packages/object/package.json
  • tuplerone ^3.3.3
  • @babel/core ^7.22.9
  • @babel/preset-env ^7.22.9
  • @babel/preset-typescript ^7.22.5
  • @types/babel__core ^7.20.1
  • @types/babel__preset-env ^7.9.2
  • @types/eslint ^8.44.0
  • @types/jest ^29.5.3
  • @types/node ^20.4.2
  • @types/prettier ^2.7.3
  • @typescript-eslint/eslint-plugin ^6.1.0
  • @typescript-eslint/parser ^6.1.0
  • esbuild ^0.19.0
  • esbuild-jest ^0.5.0
  • eslint ^8.45.0
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-jest ^27.2.3
  • eslint-plugin-jest-formatting ^3.1.0
  • eslint-plugin-tsdoc ^0.2.17
  • jest ^29.6.1
  • jest-github-reporter ^1.1.1
  • microbundle ^0.15.1
  • prettier ^3.0.0
  • node >= 18
  • node 18.17.1
packages/set/package.json
  • @babel/core ^7.22.9
  • @babel/preset-env ^7.22.9
  • @babel/preset-typescript ^7.22.5
  • @types/babel__core ^7.20.1
  • @types/babel__preset-env ^7.9.2
  • @types/eslint ^8.44.0
  • @types/jest ^29.5.3
  • @types/node ^20.4.2
  • @types/prettier ^2.7.3
  • @typescript-eslint/eslint-plugin ^6.1.0
  • @typescript-eslint/parser ^6.1.0
  • esbuild ^0.19.0
  • esbuild-jest ^0.5.0
  • eslint ^8.45.0
  • eslint-config-prettier ^9.0.0
  • eslint-plugin-jest ^27.2.3
  • eslint-plugin-jest-formatting ^3.1.0
  • eslint-plugin-tsdoc ^0.2.17
  • jest ^29.6.1
  • jest-github-reporter ^1.1.1
  • microbundle ^0.15.1
  • prettier ^3.0.0
  • node >= 18
  • node 18.17.1
nvm
.nvmrc
  • node 19.9.0

  • Check this box to trigger a request for Renovate to run again on this repository

Increase contrast for logo in dark mode

image

right now it's pretty much unreadbable πŸ˜•
a white or gray bg would get great!

I'm willing to open a pr ❀️ just want to make sure it wont be stuck in limbo πŸ˜…

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.