Reintent

@reintent/lib/program-edit

Editing a program: a change of K across time is a change of the program (principles, 「选择 C」: K grows by reprogramming).

An edit names one element of the program, by its component and its id, and sets its props: the data a component makes its possibilities from (a man's hats, the places a slide's block was pinned to). It gives a new program, the base left as it was, for the host to offer or commit as a new version through the reprogramming path a person accepts.

propsIn(text, at)            the props one element of a program's text writes as data
editProps(text, at, props)   the text with that element's props set (undefined: taken out)
editElement(program, at, f)  the same on a program that is elements, not text
applyPatch(value, ops)       a JSON Patch of a prop's data (a declaration a model patched),
                             the value it gives; the base left as it was

An edit is about one instance, never about every element of an entity: two beats are two elements, and a place one of them learned is that beat's. Named by its id, an instance written in several places (the cases of a branch) is edited in each; none is refused, and so is more than one element where no id names them (ProgramEditRefused). Nothing is merged by name.

What a component makes of its props is the component's: typed where it is written, so a text edited to hold a possibility more is another program, with its own types, and not a value outside a union the code was compiled with.

New. The text is read with Sucrase's parser, as @reintent/lib/program reads it.

Classes

ProgramEditRefused

Defined in: packages/lib/src/program-edit.ts

An edit that could not be made: where it pointed, nothing was, or more than one thing was.

Extends

  • Error

Constructors

Constructor
new ProgramEditRefused(message): ProgramEditRefused;

Defined in: packages/lib/src/program-edit.ts

Parameters
ParameterType
messagestring
Returns

ProgramEditRefused

Overrides
Error.constructor

Type Aliases

ElementAt

type ElementAt = object;

Defined in: packages/lib/src/program-edit.ts

Which element an edit is about: its component's name in the text (or the component), and its id.

Properties

PropertyModifierType
id?readonlystring
tagreadonlystring

PatchOp

type PatchOp = 
  | {
  op: "add" | "replace";
  path: string;
  value: unknown;
}
  | {
  op: "remove";
  path: string;
};

Defined in: packages/lib/src/program-edit.ts

One JSON Patch op (RFC 6902 add, remove, replace) on data: a path from the value's root.

Functions

applyPatch()

function applyPatch<T>(value, ops): T;

Defined in: packages/lib/src/program-edit.ts

A JSON Patch applied to a copy of value (the data a prop holds: a declaration a model patched), the value it gives; value is left as it was. A path that is not there throws (ProgramEditRefused); '' is the whole value, which replace replaces.

Type Parameters

Type Parameter
T

Parameters

ParameterType
valueT
opsreadonly PatchOp[]

Returns

T

Example

import { applyPatch } from '@reintent/lib/program-edit';

const hat = { kinds: { usual: { scale: 1 }, big: { scale: 1.45 } }, default: 'big' };
const more = applyPatch(hat, [
  { op: 'add', path: '/kinds/towering', value: { scale: 2.4 } },
]);
console.log(Object.keys(more.kinds), Object.keys(hat.kinds)); // → [ 'usual', 'big', 'towering' ] [ 'usual', 'big' ]

editElement()

function editElement(
   program, 
   at, 
   props
): Child;

Defined in: packages/lib/src/program-edit.ts

A program that is elements, with one element's props changed: props gives its new props from the ones it has. The element is named as in text, by its component (or its name) and id; none or more than one is refused. Elements around it are new, the rest are the same objects.

Parameters

ParameterType
programChild
at{ id?: string; tag: string | Component<any>; }
at.id?string
at.tagstring | Component<any>
props(props) => Record<string, any>

Returns

Child

Example

import { host } from '@reintent/core';
import { editElement } from '@reintent/lib/program-edit';

const Slide = (p: { id: string; pins?: Record<string, string> }) => host('slide', p);
const deck = [<Slide id="gallery" />, <Slide id="closing" />];
const next = editElement(deck, { tag: Slide, id: 'gallery' }, (p) => ({
  ...p,
  pins: { g0: 'pin-wide' },
}));
console.log((next as any)[0].props.pins, (next as any)[1] === deck[1]); // → { g0: 'pin-wide' } true

editProps()

function editProps(
   source, 
   at, 
   props
): string;

Defined in: packages/lib/src/program-edit.ts

A program's text with one element's props set, as data (undefined takes one out): the same text everywhere else. The base text is left as it was; the new one is a new version for the host to offer as a proposal.

Parameters

ParameterType
sourcestring
atElementAt
propsReadonly<Record<string, unknown>>

Returns

string

Example

import { editProps } from '@reintent/lib/program-edit';

const text = `export default (
  <Dream>
    <Beat id="b1" line="in the wardrobe" />
    <Beat id="b3" line="at the bed" />
  </Dream>
);`;
// The place b3 learned is b3's: b1 is another element.
const next = editProps(
  text,
  { tag: 'Beat', id: 'b3' },
  { places: { sill: { at: [96, 704] } } },
);
console.log(next.split('\n')[3].trim());
// → '<Beat id="b3" line="at the bed" places={{"sill":{"at":[96,704]}}} />'

propsIn()

function propsIn(source, at): Record<string, unknown>;

Defined in: packages/lib/src/program-edit.ts

The props one element of a program's text writes as data (strings, and JSON in braces); a prop written as code is undefined.

Parameters

ParameterType
sourcestring
atElementAt

Returns

Record<string, unknown>

Example

import { propsIn } from '@reintent/lib/program-edit';

const text = `export default <Dream><Man id="man" hat={{"kinds":["usual","big"]}} /></Dream>;`;
console.log(propsIn(text, { tag: 'Man', id: 'man' }));
// → { id: 'man', hat: { kinds: [ 'usual', 'big' ] } }

Last updated on

Edit on GitHub