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

@kizahasi/ot-string

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kizahasi/ot-string - npm Package Compare versions

Comparing version 0.6.0-beta.2 to 0.6.0-beta.3

8

dist/cjs/internal/operation/upOperation.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ofUnit = exports.toUnit = exports.invert = exports.transform = exports.compose = exports.applyAndRestore = exports.apply = void 0;
const option_1 = require("@kizahasi/option");
const result_1 = require("@kizahasi/result");

@@ -11,7 +10,4 @@ const ot_core_1 = require("@kizahasi/ot-core");

insert: ({ state, start, replacement }) => {
var _a, _b;
return {
newState: state.substring(0, start) +
((_b = (_a = replacement.value) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : '') +
state.substring(start),
newState: state.substring(0, start) + replacement.value + state.substring(start),
};

@@ -54,3 +50,3 @@ },

factory: operationBuilderFactory_1.twoWayFactory,
mapping: ({ actual }) => option_1.Option.some(actual),
validateDeleted: ({ actual }) => result_1.Result.ok(actual),
});

@@ -57,0 +53,0 @@ if (result.isError) {

@@ -1,2 +0,1 @@

import { Option } from '@kizahasi/option';
import { Result } from '@kizahasi/result';

@@ -8,7 +7,4 @@ import { r, i, d, insert$, delete$, retain, apply as applyCore, applyAndRestore as applyAndRestoreCore, compose as composeCore, transform as transformCore, invert as invertCore, PositiveInt, OperationBuilder, } from '@kizahasi/ot-core';

insert: ({ state, start, replacement }) => {
var _a, _b;
return {
newState: state.substring(0, start) +
((_b = (_a = replacement.value) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : '') +
state.substring(start),
newState: state.substring(0, start) + replacement.value + state.substring(start),
};

@@ -50,3 +46,3 @@ },

factory: twoWayFactory,
mapping: ({ actual }) => Option.some(actual),
validateDeleted: ({ actual }) => Result.ok(actual),
});

@@ -53,0 +49,0 @@ if (result.isError) {

@@ -1,2 +0,1 @@

import { Option } from '@kizahasi/option';
import { Result } from '@kizahasi/result';

@@ -47,6 +46,3 @@ import {

return {
newState:
state.substring(0, start) +
(replacement.value?.value ?? '') +
state.substring(start),
newState: state.substring(0, start) + replacement.value + state.substring(start),
};

@@ -105,3 +101,3 @@ },

factory: twoWayFactory,
mapping: ({ actual }) => Option.some(actual),
validateDeleted: ({ actual }) => Result.ok(actual),
});

@@ -108,0 +104,0 @@ if (result.isError) {

{
"name": "@kizahasi/ot-string",
"version": "0.6.0-beta.2",
"version": "0.6.0-beta.3",
"description": "Operational Transfomation library for string",

@@ -25,6 +25,5 @@ "keywords": [

"dependencies": {
"@kizahasi/option": "^1.0.0",
"@kizahasi/result": "^1.0.0",
"diff-match-patch": "^1.0.5",
"@kizahasi/ot-core": "0.1.0-beta.2"
"@kizahasi/ot-core": "0.1.0-beta.3"
},

@@ -31,0 +30,0 @@ "devDependencies": {

@@ -1,123 +0,5 @@

# @kizahasi/ot-string
Please see [/README.md](/README.md).
![GitHub](https://img.shields.io/github/license/kizahasi/ot-string) [![npm version](https://img.shields.io/npm/v/@kizahasi/ot-string.svg?style=flat)](https://www.npmjs.com/package/@kizahasi/ot-string) ![minified size](https://img.shields.io/bundlephobia/min/@kizahasi/ot-string) [![CI](https://github.com/kizahasi/ot-string/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/kizahasi/ot-string/actions/workflows/ci.yml) [![publish](https://github.com/kizahasi/ot-string/actions/workflows/publish.yml/badge.svg?branch=release)](https://github.com/kizahasi/ot-string/actions/workflows/publish.yml)
## License
Operational Transfomation library for string.
## Installation
Run `npm install @kizahasi/ot-string` or `yarn add @kizahasi/ot-string`.
To use it in a browser directly, you can use [Skypack](https://www.skypack.dev/view/@kizahasi/ot-string).
## Usage
### Diff two texts
```javascript
import { diff } from '@kizahasi/ot-string';
const prevState = 'January';
const nextState = 'February';
const twoWayOperation = diff({ prevState, nextState });
```
### Apply operations
```javascript
import { diff, toUpoperation, apply, toDownOperation, applyBack } from '@kizahasi/ot-string';
const prevState = 'January';
const nextState = 'February';
const twoWayOperation = diff({ prevState, nextState });
// `toUpOperation` drops some data from twoWayOperation, but its object size is reduced.
const upOperation = toUpOperation(twoWayOperation);
const nextState2 = apply({ prevState, upOperation });
console.log(nextState2.isError, nextState2.value);
// => false February
// `toDownOperation` drops some data from twoWayOperation, but its object size is reduced.
const downOperation = toDownOperation(twoWayOperation);
const prevState2 = applyBack({ prevState, upOperation });
console.log(nextState2.isError, nextState2.value);
// => false January
```
### Operational transformation
```javascript
import { toUpOperation, diff, transformUpOperation, apply } from '@kizahasi/ot-string';
const state1 = 'June 1';
const state2_june2 = 'June 2';
const state2_july1 = 'July 1';
const first = toUpOperation(diff({ prevState: state1, nextState: state2_june2 }));
const second = toUpOperation(diff({ prevState: state1, nextState: state2_july1 }));
const transformed = transformUpOperation({ first, second });
console.log(transformed.isError);
// => false
// state1 + first + secondPrime
const state3a = apply({ prevState: state2_june2, upOperation: transformed.value.secondPrime });
// state1 + second + firstPrime
const state3b = apply({ prevState: state2_july1, upOperation: transformed.value.firstPrime });
console.log(state3a.isError);
// => false
console.log(state3b.isError);
// => false
console.log(state3a.value === 'July 2');
// => true
// state1 + first + secondPrime = state1 + second + firstPrime
console.log(state3a.value === state3b.value);
// => true
```
### Serialization and deserialization
```javascript
import {
diff,
toDownOperation,
toUpOperation,
serializeUpOperation,
serializeDownOperation,
deserializeUpOperation,
deserializeDownOperation,
deserializeTwoWayOperation,
} from '@kizahasi/ot-string';
const twoWayOperation = diff({ prevState: 'hour', nextState: 'ours' });
const upOperation = toUpOperation(twoWayOperation);
const downOperation = toDownOperation(twoWayOperation);
// Serialize UpOperation.
const serializedUpOperation = serializeUpOperation(upOperation);
console.log(serializedUpOperation);
// => [ { t: 'd', d: 1 }, { t: 'r', r: 3 }, { t: 'i', i: 's' } ]
// (t = type, r = retain, i = insert, d = delete. Above object indicates "Delete 1 character, then retain 3 characters, finally insert 's'.")
// Serialize DownOperation.
const serializedDownOperation = serializeDownOperation(downOperation);
console.log(serializedDownOperation);
// => [ { t: 'd', d: 'h' }, { t: 'r', r: 3 }, { t: 'i', i: 1 } ]
// Serialize TwoWayOperation.
const serializedTwoWayOperation = serializeTwoWayOperation(downOperation);
console.log(serializedDownOperation);
// => [ { t: 'd', d: 'h' }, { t: 'r', r: 3 }, { t: 'i', i: 's' } ]
// Deserialize.
const deserializedUpOperation = deserializeUpOperation(serializedUpOperation);
const deserializedDownOperation = deserializeDownOperation(serializedDownOperation);
const deserializedTwoWayOperation = deserializeTwoWayOperation(serializedTwoWayOperation);
```
## Issues
- Some functions are not implemented (e.g. transformDownOperation).
MIT

Sorry, the diff of this file is not supported yet

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