Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@tsrt/ordering

Tools for common ordering operations with arrays

ordering, sorting, reorder, reorder-list

readme

Typescript Reusable Tools: Ordering

npm version GitHub license Size Downloads

Lib for common ordering operations with arrays.

Important

Until version 1.0.0 Api should be considered as unstable and may be changed.

So prefer using exact version instead of version with ~ or ^.

Usage

import { OrderingService, IOrderingServiceConfig } from '@tsrt/ordering';

interface IOrderedItem { pk: string; order: number };

// Or use default IOrderingItemDefault from package, for example.
// Or redeclare IOrderingItemDefault via Typescipt module augumentation.
// https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation

const config: IOrderingServiceConfig = { ... }
const orderingService = new OrderingService<IOrderedItem>(config);

const initialArray = [{ id: 'A', order: 0 }, { id: 'B', order: 14 }, { id: 'C', order: 22 }];
const orderChanges = [{ id: 'A', order: 15 }, { id: 'C', order: 2 }];

// It will reorder [A, B, C] into [B, A, C] -> [C, B, A], `order` properties will be updated accordingly.
const result = orderingService.reorder(initialArray, orderChanges);

// It will reorder [A, B, C] into [B, C, A], `order` properties will be updated accordingly.
const result = orderingService.reorderByIndex(initialArray, 0, 2);

// Just move item ia array from prevIndex to newIndex. DO NOT updates `order` properties.
const result = orderingService.moveItemInArray(initialArray, 0, 2);

// Here we can check whether it is necessary to perform reordering.
const result = orderingService.hasDuplicateOrEmptyOrders(initialArray);

// Here we can check whether there are any invalid items for reordering inside array.
const result = orderingService.hasInvalidOrderingItems(initialArray);

API reference

OrderingService
export declare class OrderingService<T extends GenericObject = IOrderingItemDefault> {
  /**
   *  Reorders target applying orders from listOfOrdersChanges one by one.
   *
   *  @param target - Original array.
   *  @param listOfOrdersChanges - List of order changes like: [{ `primaryKey`: value, `oderKey`: newOrderValue }]
   *
   *  @returns list, reordered according to provided order changes.
   */
  reorder<I extends T = T>(target: I[], listOfOrdersChanges?: Array<Required<T>>, config?: IOrderingOptions): Array<I & Required<T>>;

  /**
   *  Reorders target, depending on position of reordered item.
   *  Returns new array and does not mutate original array unless `updateTarget` falg is provided.
   *
   *  @param target - Original array.
   *  @param prevIndex - Item to reorder prev (from) index.
   *  @param newIndex - Item to reorder new (to) index.
   *  @param [updateTarget] - Whether to mutate original array.
   */
  reorderByIndex<I extends Required<T> = Required<T>>(target: I[], prevIndex: number, newIndex: number, updateTarget?: boolean): I[];

  /**
   *  Moves item form prevIndex to newIndex inside array.
   *  Returns new array and does not mutate original array unless `updateTarget` falg is provided.
   *
   *  @param target - Original array.
   *  @param prevIndex - Item to move prev (from) index.
   *  @param newIndex - Item to move new (to) index.
   *  @param [updateTarget] - Whether to mutate original array.
   */
  moveItemInArray<I extends T = T>(target: I[], prevIndex: number, newIndex: number, updateTarget?: boolean): I[];

  /**
   *  Checks whether provided array has items without order / empty orders. If has - returns first such item.
   *
   *  @param target - Target array.
   */
  hasDuplicateOrEmptyOrders<I extends T = T>(target: I[]): I;

  /**
   *  Checks whether provided array has invalid items. If has - returns first such item.
   *
   *  Invalid if at least for 1 item inside array one of next conditions is true:
   *  - item is null/undefined/not object;
   *  - item has no `primarKey` property;
   *  - item has no `orderKey` property (only is `strict` mode);
   *  - item `orderKey` property is not null/undefined and not number;
   *  - item `orderKey` property is not null/undefined and less than 0;
   *
   *  @param target - Target array.
   *  @param [strict=false] - Whether to throw an Error if there is no `order` property for at least 1 item.
   */
  hasInvalidOrderingItems<I extends T = T>(target: I[], strict?: boolean): I;
}
Aliases

Is is also possible to import aliases for all OrderingService public methods as functions.

This functions will work with default config:

import { reorder, reorderByIndex, moveItemInArray, hasDuplicateOrEmptyOrders, hasInvalidOrderingItems } from '@tsrt/ordering';

Options

/** Options for OrderingService reorder method. */
export interface IOrderingOptions {
  /** If false, will throw Error if `newOrder` is out of range [min, ..., max] of existing orders. Default: false. */
  allowOrdersOutOfRange?: boolean;

  /** Whether to clamp `newOrder` into range [min, ..., max] of existing orders. Default: false. */
  clampRange?: boolean;

  /**
   *  Whether to insert `empty` or `duplicate` orders only after `max` order of existing orders. Default: false.
   *  If false, will find the minimal unique order inside range [min, ..., max] of existing orders.
   */
  insertAfterOnly?: boolean;

  /** Whether to refresh orders' sequence after reordering (start from zero). Default: false. */
  refreshSequence?: boolean;
}

/** Options for OrderingService constructor. */
export interface IOrderingServiceOptions extends IOrderingOptions {
  /** Primary key to identify entities inside array for reordering. Default: 'id'. */
  primaryKey?: string;

  /** Order key to reorder entities inside array by. Default: 'order'. */
  orderKey?: string;
}

/**
 *  Default ordering item.
 *  Empty interface for TypeScript module augumentation in importing module.
 *
 *  Example: declare module '@tsrt/ordering' { export interface IOrderingItemDefault { pk: number; order?: number } }
 *  @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation.
 */
export interface IOrderingItemDefault { }

Default options:

export const defaultOptions: IOrderingServiceOptions = {
  primaryKey: 'id',
  orderKey: 'order',
  allowOrdersOutOfRange: false,
  clampRange: false,
  insertAfterOnly: false,
  refreshSequence: false,
};

License

This project is licensed under the terms of the MIT license.

changelog

0.4.1 - 0.4.4

Bugfixes

  • [@tsrt/sequelize] Updated docs - added keywords, description, examples for sort, limit, filter/where and include repository options.
  • [@tsrt/sequelize] Minor updates.

  • [@tsrt/ordering] Updated package.json - keywords and description.
  • [@tsrt/ordering] Added tests.

  • [@tsrt/maprty] Updated package.json description for @tsrt/mparty, @tsrt/mparty-aws and @tsrt/mparty-express.

0.4.0

New features

  • [@tsrt/ordering] Moved OrderingService to separate package.
  • [@tsrt/ordering] Added clampRange, insertAfterOnly and refreshSequence options.

  • [@tsrt/sequelize] Added onBeforeRestore and onBeforeUpdateItemsOrder hooks.
  • [@tsrt/sequelize] Improved working w/ nested transaction.
  • [@tsrt/sequelize] Added tests for CustomRepository which extends BaseRepository and Hooks.
  • [@tsrt/sequelize] Improved docs.

Bugfixes

  • [@tsrt/utils] Fixed parseTypes method: parsing numbers.

Breaking changes

  • [@tsrt/sequelize] Renamed returnData flag into returnAssociations.
  • [@tsrt/sequelize] Renamed onBeforeInsertAssociatedWithEntityDataFromBody hook into onBeforeInsertAssociations.

0.3.0

New features

  • [@tsrt/utils] Totally refactored reordering utils. Old utils are now marked as deprecated and will be removed in future.
  • [@tsrt/utils] New customizable OrderingService w/ optimized and improved abilities (maybe will be moved to separate package in future).
  • [@tsrt/utils] OrderingService accepts GenericOrderingItemType and options for primaryKey, orderKey, allowOrdersOutOfRange.

  • [@tsrt/sequelize] Updated signatures update/onBeforeUpdate/onBeforeCreate/onBeforeBulkCreate methods.
  • [@tsrt/sequelize] Added ability for update method to proceed not only w/ primaryKey, but also updateOptions insead. For example for multiple rows updates.
  • [@tsrt/sequelize] Finished bulkCreate method to work properly inside transactions.
  • [@tsrt/sequelize] Added restrictedProperties configurable and default Repository options.
  • [@tsrt/sequelize] Utilizes usage of new customizable OrderingService.
  • [@tsrt/sequelize] Refactored Database class. Now it behaves as factory for database connections.
  • [@tsrt/sequelize] Wrapped all methods (not only bulk) in transactions.
  • [@tsrt/sequelize] Improved types.
  • [@tsrt/sequelize] Improved docs.
  • [@tsrt/sequelize] Added tests for Database and BaseRepository.

0.2.6

Bugfixes

  • [@tsrt/sequelize] Fixed types for onBeforeRead hook.
  • [@tsrt/sequelize] Fixed readOptions applying order.

0.2.5

New features

  • [@tsrt/sequelize] Updated signature for read/readOne/readMany methods to provide appropriate ability to use native Sequelize FindOptions.
  • [@tsrt/sequelize] Updated signature for delete/softDelete/forceDelete methods to provide ability delete by id or only w/ deleteOptions.

0.2.4

New features

  • [@tsrt/application] Added docs.

0.2.2 - 0.2.3

New features

  • [@tsrt/mparty] Improved docs.
  • [@tsrt/mparty-aws] Improved docs.
  • [@tsrt/mparty-express] Improved docs.
  • [@tsrt/sequelize] Updated types for read options.
  • [@tsrt/sequelize] Added docs.

0.2.1

New features

  • [@tsrt/mparty-aws] Added Mparty AWS S3 Adapter and docs.
  • [@tsrt/mparty-express] Added Mparty Express middleware and docs.

0.2.0

New features

  • [@tsrt/mparty] Added framework agnostic package for handling multipart/form-data requests based on busboy
  • [@tsrt/mparty] Added default FileSystem Adapter
  • [@tsrt/mparty] Added files and fields basic validations

0.1.2

New features

  • [@tsrt/web-components] Fixed handling for cases where root container is svg element itself
  • [@tsrt/web-components] Fixed bundles: now there are umd and esm bundles available

0.1.1

  • Added LICENSE
  • Added CHANGELOG.md
  • Added .npmignore
  • Added skipLibCheck: true into base tsconfig.json due to breaking changes of @types/express-session patch release
  • Added minimal Readme.md for all packages
  • Changed homepage in package.json for each package
  • Changed typescript and tslib dependencies to latest stable versions
  • Removed importHelpers from base tsconfig.json

0.1.0

  • Deployed initial packages