Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@vyacheslav97/ct

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vyacheslav97/ct - npm Package Compare versions

Comparing version 0.0.13 to 0.0.14

dist/implementations/algorithms/binarySearch.d.ts

25

package.json
{
"type": "module",
"name": "@vyacheslav97/ct",
"version": "0.0.13",
"version": "0.0.14",
"description": "",
"keywords": [
"tools",
"common tools",
"standard tools",
"iterable converter",
"graph",
"tree",
"algorithms",
"binary search",
"duplicates search"
],
"exports": {

@@ -30,2 +41,14 @@ "./uic": {

"types": "./dist/interfaces/graphs/interfaces.d.ts"
},
"./mutationsHistory": {
"import": "./dist/implementations/MutationsHistory/MutationsHistory.js",
"types": "./dist/implementations/MutationsHistory/MutationsHistory.d.ts"
},
"./algorithms/binarySearch": {
"import": "./dist/implementations/algorithms/binarySearch.js",
"types": "./dist/implementations/algorithms/binarySearch.d.ts"
},
"./algorithms/duplicatesSearcher": {
"import": "./dist/implementations/algorithms/duplicatesSearcher.js",
"tyoes": "./dist/implementations/algorithms/duplicatesSearcher.d.ts"
}

@@ -32,0 +55,0 @@ },

@@ -9,10 +9,9 @@ # <u>Common tools (ct, also @vyacheslav97/ct)</u>

- [Graph abstract class](#GraphAbstractClass)
- [Tree abstract class](TreeAbstractClass)
- [MutationsHistory abstract class](#mutationsHistory)
- [Binary search procedure template](#binarySearch)
- [Duplicates search procedure template](#duplicatesSearch)
Next scheduled updates:
- History abstract class
- Binary search procedure template
- Unique values searcher procedure template
- Numbers validation methods: various standard regexp, **isNumber**
- Default pathfinder template based on BFS algorithm

@@ -31,3 +30,6 @@ - Default leveling step function based on DFS algorithm

- [Graph abstract class](#GraphAbstractClass)
- [Tree abstract class](TreeAbstractClass)
- [Tree abstract class](#TreeAbstractClass)
- [Mutations history template class](#mutationsHistory)
- [Binary search procedure template](#binarySearch)
- [Duplicates search procedure template](#duplicatesSearch)

@@ -90,2 +92,3 @@ <a id="uic"></a>

<a id="uicsds"></a>
## Universal iterable converter (standard data structures, UICSDS)

@@ -387,1 +390,186 @@

```
<a id="mutationsHistory"></a>
## Mutations history template class
**MutationsHistory** template class import:
```ts
import MutationsHistory from '@vyacheslav97/ct/mutationsHistory';
```
**MutationsHistory** implements changes history abstract mechanism. It is described via following interface:
```ts
interface MutationsHistoryInterface<HistoryUnitType> {
lastSavedChangeIndex: number,
commitedMutations: HistoryUnitType[],
canceledMutations: HistoryUnitType[],
getCurrentMutation: () => HistoryUnitType | undefined,
addMutation: (mutation: HistoryUnitType) => void,
cancelLastMutation: () => void,
restoreCanceledMutation: () => void,
saveCurrentMutation: () => void,
backToSavedMutation: () => void,
saveAndClear: () => void,
clearHistory: () => void,
}
```
### MutationshHistory API
***Data:***
- **commitedMutations** stack - stack of committed changes
- **canceledMutations** stack - stack of canceled changes
- **lastSavedChangeIndex** - index of last saved change
***Methods:***
- **getCurrentMutation** - returns the most recent committed change
- **addMutation** - adds a fresh mutation to **commitedMutations** stack. Devastates filled **canceledMutations** stack
- **cancelLastMutation** - removes the most recent mutation from **commitedMutations** stack and places it in **canceledMutations** stack
- **restoreCanceledMutation** - if there are canceled mutations, then removes fresh mutation from **canceledMutations** stack and moves it to **commitedMutations** stack
- **saveCurrentMutation** - updates **lastSavedChangeIndex** with commitedMutations.length - 1
- **backToSavedMutation** - cancels or restores all mutations up to the last saved
- **saveAndClear** - saves the most recent commited mutation and demolishes the rest (canceled inclusive)
- **clearHistory** - clears history
A **MutationsHistory** instance can be created using predefined committed history list via **MutationsHis** class constructor:
```ts
constructor(sourceIterable?: Iterable<HistoryUnitType>) {
this.commitedMutations = sourceIterable ? [...sourceIterable]: [];
this.canceledMutations = [];
this.lastSavedChangeIndex = this.commitedMutations.length - 1;
}
```
**MutationsHistory** is able to store whatever you plan it to: from changed objects itself to actions that mutate certain data structure. But a whole template, that rules history rules, remains the same.
<a id="binarySearch"></a>
## Binary search procedure template
**Binary search** template procedure import:
```ts
import {
binarySearch,
buildBinarySearch,
} from '@vyacheslav97/ct/algorithms/binarySearch';
```
**NOTE:** it is highly recommended to prefer **buildBinarySearch** over **binarySearch**.
**binarySearch** implements well-known binary search algorithm for array of arbitrary content. It receives:
- **sourceList** - list of data for binary search (list cotains data of the same arbitrary type)
- **target** - binary search target
- **comparator** - function that compares array elements and has a result described via following interface:
```ts
interface ComparatorResult {
equal?: boolean,
less?: boolean,
greater?: boolean,
}
```
**binarySearch** returns target index, if target does exist on given array or **undefined** otherwise.
**buildBinarySearch** creates **binarySearch** instance with a given comparator:
```ts
const numbersComparator = (x: number, y: number): ComparatorResult => ({
less: x < y,
greater: x > y,
equal: x === y,
});
const numericalBinarySearch = buildBinarySearch(numbersComparator);
numericalBinarySearch([], 5); // undefined
numericalBinarySearch([1, 2, 3, 4, 5], 2); // 1
```
<a id="duplicatesSearch"></a>
## Duplicates search procedure template
**Duplicates search** procedure template import:
```ts
import {
duplicatesSearcher,
buildDuplicatesSearcher,
} from '@vyacheslav97/ct/algorithms/duplicatesSearcher';
```
**duplicatesSearcher** function looks for duplicates within a given array of data. It accepts following arguments:
- **source** – duplications source iterable
- **valueExtractor** – optional function, derives a primitive according which two values are considered as duplicates
- Returns **Map**, which keys are **valueExtractor** values, values are lists of **DuplicateData**:
```ts
interface DuplicateData<T> {
duplicateIndex: number;
duplicateValue: T;
}
```
**Note:** **duplicatesSearcher** returns empty **Map** only if source iterable is empty, otherwise it has maximum size equal to source iterable values amount.
**buildDuplicatesSearcher** builds a **duplicatesSearcher** instance with a given**valueExtractor**:
```ts
interface SimpleObjectData {
value: string,
}
const valueExtractor = (data: SimpleObjectData) => data.value;
const numericalDuplicatesSearcher = buildDuplicatesSearcher<number>();
numericalDuplicatesSearcher([1, 2, 3, 4, 5]);
export const objectDuplicatesSearcher = buildDuplicatesSearcher(valueExtractor);
objectDuplicatesSearcher([
{
value: 'a',
},
{
value: 'a',
},
{
value: 'c',
},
]);
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc