CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-prelude-ls

A functionally oriented utility library for LiveScript with curried functions for lists, objects, strings, numbers, and function composition.

89

1.53x
Overview
Eval results
Files

object-operations.mddocs/

Object Operations

Object manipulation utilities for converting between objects and arrays, iterating over properties, and transforming object structures. The Object module provides 14 functions for comprehensive object handling.

Capabilities

Object Conversion

Functions for converting between objects and various array representations.

/**
 * Gets array of object values
 * @param {Object} object - Object to extract values from
 * @returns {Array} Array of object values
 */
function values(object);

/**
 * Gets array of object keys
 * @param {Object} object - Object to extract keys from
 * @returns {Array<string>} Array of object keys
 */
function keys(object);

/**
 * Converts array of [key, value] pairs to object
 * @param {Array<Array>} pairs - Array of [key, value] pairs
 * @returns {Object} Object created from pairs
 */
function pairsToObj(pairs);

/**
 * Converts object to array of [key, value] pairs
 * @param {Object} object - Object to convert
 * @returns {Array<Array>} Array of [key, value] pairs
 */
function objToPairs(object);

/**
 * Creates object from separate keys and values arrays
 * @param {Array<string>} keys - Array of keys
 * @param {Array} values - Array of values
 * @returns {Object} Object with paired keys and values
 */
function listsToObj(keys, values);

/**
 * Converts object to [keys, values] arrays
 * @param {Object} object - Object to convert
 * @returns {Array} [keysArray, valuesArray]
 */
function objToLists(object);

Object Operations

Functions for manipulating and querying objects.

/**
 * Checks if object has no properties
 * @param {Object} object - Object to check
 * @returns {boolean} True if object has no enumerable properties
 */
function empty(object);

/**
 * Applies function to each value, returns original object
 * @param {Function} fn - Function to apply to each value
 * @param {Object} object - Object to iterate over
 * @returns {Object} Original object (for chaining)
 */
function each(fn, object);

/**
 * Transforms each value with function
 * @param {Function} fn - Transformation function
 * @param {Object} object - Object to transform
 * @returns {Object} New object with transformed values
 */
function map(fn, object);

/**
 * Removes properties with falsy values
 * @param {Object} object - Object to compact
 * @returns {Object} Object without falsy-valued properties
 */
function compact(object);

/**
 * Keeps properties where value matches predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Object} object - Object to filter
 * @returns {Object} Object with only matching properties
 */
function filter(predicate, object);

/**
 * Removes properties where value matches predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Object} object - Object to filter
 * @returns {Object} Object with non-matching properties removed
 */
function reject(predicate, object);

/**
 * Splits object into [passed, failed] based on predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Object} object - Object to partition
 * @returns {Array<Object>} [passedObject, failedObject]
 */
function partition(predicate, object);

/**
 * Finds first value matching predicate
 * @param {Function} predicate - Function returning boolean
 * @param {Object} object - Object to search
 * @returns {*} First matching value or undefined
 */
function find(predicate, object);

Usage Examples

Object-Array Conversion:

const { keys, values, objToPairs, pairsToObj } = require('prelude-ls');

const user = { name: 'Alice', age: 30, city: 'Boston' };

const userKeys = keys(user);           // ['name', 'age', 'city']
const userValues = values(user);       // ['Alice', 30, 'Boston']
const userPairs = objToPairs(user);    // [['name', 'Alice'], ['age', 30], ['city', 'Boston']]

// Reconstruct object
const reconstructed = pairsToObj(userPairs); // { name: 'Alice', age: 30, city: 'Boston' }

Object Transformation:

const { map, filter, keys } = require('prelude-ls');

const scores = { math: 85, english: 92, science: 78 };

// Transform values
const curved = map(score => score + 5, scores);
// Result: { math: 90, english: 97, science: 83 }

// Filter by value
const highScores = filter(score => score > 90, curved);
// Result: { english: 97 }

// Get subject names with high scores
const highSubjects = keys(highScores); // ['english']

Object Property Operations:

const { compact, partition, find } = require('prelude-ls');

const data = { 
  valid: 'data', 
  empty: '', 
  zero: 0, 
  text: 'hello',
  nullValue: null 
};

// Remove falsy values
const cleaned = compact(data);
// Result: { valid: 'data', text: 'hello' }

// Partition by string values
const [strings, nonStrings] = partition(
  value => typeof value === 'string', 
  data
);
// strings: { valid: 'data', empty: '', text: 'hello' }
// nonStrings: { zero: 0, nullValue: null }

// Find first string value
const firstString = find(value => typeof value === 'string', data);
// Result: 'data'

Lists and Objects Integration:

const { listsToObj, objToLists } = require('prelude-ls');

const subjects = ['math', 'english', 'science'];
const grades = ['A', 'B', 'A'];

// Create object from parallel arrays
const gradebook = listsToObj(subjects, grades);
// Result: { math: 'A', english: 'B', science: 'A' }

// Extract back to parallel arrays
const [subjectList, gradeList] = objToLists(gradebook);
// subjectList: ['math', 'english', 'science']
// gradeList: ['A', 'B', 'A']

Curried Object Processing:

const { map, filter } = require('prelude-ls');

// Create reusable object processors
const incrementValues = map(x => x + 1);
const keepPositive = filter(x => x > 0);

const numbers = { a: 1, b: -2, c: 3, d: 0 };

const processed = keepPositive(incrementValues(numbers));
// Result: { a: 2, c: 4 }

Object Inspection:

const { empty, keys, values } = require('prelude-ls');

const user = { name: 'Alice', preferences: {} };

const isEmpty = empty(user.preferences);  // true
const hasData = !empty(user);            // true

const fieldCount = keys(user).length;    // 2
const hasName = values(user).includes('Alice'); // true

Install with Tessl CLI

npx tessl i tessl/npm-prelude-ls

docs

function-utilities.md

index.md

list-operations.md

mathematical-operations.md

object-operations.md

string-processing.md

tile.json