1.1.3 • Published 3 years ago

@nmsp/line-items v1.1.3

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

@nmsp/line-items

Overview

@nmsp/line-items contains some handy utility functions for manipulating line-item data. Line item data is best stored in state management (i.e. redux).

Installation

npm install @nmsp/line-items

Usage

Each utility function can be imported as a named import. For example, import { add } from '@nmsp/line-items'.

Available functions

add

Adds a new line item to an existing line item list with the specified quantity. If the line item already exists, the line item quantity will increase by quantity specified. Returns a new list of line items.

Syntax

const newLineItems = add(variantId, quantity, existingLineItems)

Parameters

ParameterDescription
variantIdThe identifier for the line item to be added.
quantityThe quantity of the variant to be added (defaults to 1).
existingLineItemsThe existing line items list.

remove

Removes a line item from a line item list. Returns a new list of line items.

Syntax

const newLineItems = remove(variantId, existingLineItems)

Parameters

ParameterDescription
variantIdThe identifier for the line item to be removed.
existingLineItemsThe existing line items list.

increment

Increments an existing line item. Returns a new list of line items.

Syntax

const newLineItems = increment(variantId, existingLineItems)

Parameters

ParameterDescription
variantIdThe identifier for the line item to be incremented.
existingLineItemsThe existing line items list.

decrement

Decrements an existing line item. If the decremented line item quantity is zero, the line item will be removed from the line item list. Returns a new list of line items.

Syntax

const newLineItems = decrement(variantId, existingLineItems)

Parameters

ParameterDescription
variantIdThe identifier for the line item to be decremented.
existingLineItemsThe existing line items list.

updateQuantity

Updates the quantity of an existing line item. If the quantity is zero, the line item is removed. Returns a new list of line items.

Syntax

const newLineItems = updateQuantity(variantId, quantity, existingLineItems)

Parameters

ParameterDescription
variantIdThe identifier for the line item to be updated.
quantityThe quantity to update the existing variant.
existingLineItemsThe existing line items list.