项目作者: samvv

项目描述 :
A standard collections library for JavaScript/TypeScript
高级语言: TypeScript
项目地址: git://github.com/samvv/scl.js.git
创建时间: 2020-05-02T14:38:31Z
项目社区:https://github.com/samvv/scl.js

开源协议:MIT License

下载


Coverage Status

This is a curated, open-source project of common JavaScript collections with
full support for TypeScript. Initially started as a side-project to abstract
away some common patterns in other projects, this library continues to grow to
become a full standard library of efficient algorithms.

  1. npm i scl

☝️ We could use a helping hand. If you think you’re up for it,
open an issue.

📖 Go straight to the documentation!

Examples

Using the priority queue to sort some tasks on importance

  1. import { PriorityQueue } from "scl"
  2. interface Task {
  3. priority: number
  4. description: string
  5. }
  6. const tasks = new PriorityQueue<Task>({
  7. compare: (a, b) => a.priority < b.priority
  8. })
  9. tasks.add({ description: 'Do the dishes', priority: 5 })
  10. tasks.add({ description: 'Buy food', priority: 1 })
  11. tasks.add({ description: 'Play some games', priority: 52 })
  12. tasks.add({ description: 'Go for a walk', priority: 10 })
  13. tasks.add({ description: 'Program like crazy', priority: 20 })
  14. // Take the most important task from the queue
  15. const buyFood = tasks.pop();
  16. // See what the next task looks like without removing it
  17. const doTheDishes = tasks.peek()
  18. console.log('I should do the remaining tasks in the following order:');
  19. for (const task of tasks) {
  20. console.log(`- ${task.description}`);
  21. }

This will output the following text:

  1. I should do the remaining tasks in the following order:
  2. - Do the dishes
  3. - Go for a walk
  4. - Program like crazy
  5. - Play some games

Sorting and querying a list of people based on their age

  1. import { TreeIndex } from "scl"
  2. interface Person {
  3. name: string;
  4. email: string;
  5. age: number;
  6. }
  7. const people = new TreeIndex<Person, number>([
  8. {
  9. name: 'Bob',
  10. email: 'thebobman@gmail.com',
  11. age: 45,
  12. },
  13. {
  14. name: 'Fred',
  15. email: 'fred@outlook.com',
  16. age: 33,
  17. },
  18. {
  19. name: 'Lisa',
  20. email: 'lisa.turner@gmail.com',
  21. age: 37,
  22. }
  23. ]);
  24. // Lisa is the oldest person who is at the very most 40 years old.
  25. const lisa = people.getGreatestLowerBound(40);
  26. // Bob is the youngest person older than Lisa
  27. const bob = lisa.next();
  28. // No one is older than Bob
  29. assert(bob.next() === null);

Storing many different translations in the same dictionary

  1. import { TreeMultiDict } from "scl"
  2. const d = new TreeMultiDict<number, string>([
  3. [1, 'Ein'],
  4. [2, 'dos'],
  5. [1, 'uno'],
  6. [2, 'Zwei'],
  7. [2, 'duo'],
  8. ])
  9. const oneInDifferentLanguages = [...d.getValues(1)];
  10. for (const word of oneInDifferentLanguages) {
  11. console.log(`The number 1 can be translated as '${word}'`);
  12. }
  13. const [added, threeCursor] = d.emplace(3, 'tres')
  14. if (d.hasKey(3)) {
  15. console.log(`The dictionary now has 3 in its keys.`);
  16. } else {
  17. console.log(`The dictionary does not contain 3.`);
  18. }
  19. console.log(`The dictionary now has ${d.size} elements.`)
  20. d.deleteAt(threeCursor)

The output of the above program:

  1. The number 1 can be translated as as 'uno'
  2. The number 1 can be translated as as 'Ein'
  3. The dictionary now has 3 in its keys.
  4. The dictionary now has 6 elements.

Usage

The sources in this library target a relatively new ECMAScript version, so that
you are able to choose how much backwards-compatible the generated JavaScript
should be. You are expected to use this library with a bundler such as
Webpack or Rollup. Recent versions of NodeJS should also work without
any bundler.

There is experimental support for tree shaking, which will result in much
smaller JavaScript bundles. If you encounter an issue with this, please take
your time to report it.

Documentation

All collections are documented using TypeDoc, and the latest
documentation is available here
.

If you’ve found a mistake in the documentation or something is not quite clear,
don’t hesitate to open an issue.

Support

Found an issue? A certain mistake? Need a certain kind of collection? File an
issue
or send me a pull request.

Credits

Thanks to Wolfgang De Meuter’s introductory course to algorithms and data
structures for teaching many of the concepts that are used in this library.

Many thanks to @w8r for providing a reference implementation of the AVL-tree data structure.

License

The MIT License