thesmo-calc-fields-arraydeprecated

Collection of calculating fields for array values

Usage no npm install needed!

<script type="module">
  import thesmoCalcFieldsArray from 'https://cdn.skypack.dev/thesmo-calc-fields-array';
</script>

README

npm npm GitHub GitHub last commit

Summary

This package contains the collection of useful calculating fields for arrays.

API

API methods clarification

Add

Signature

/* arguments object */
({ 
    payload: *
})
/* returns */
(source: *[]) => []

Usage example

const { Add } = require("thesmo-calc-fields-array");

const testArray = [1,2,3];

// [1, 2, 3, 4, 5]
const a = Add({payload: [4, 5]})(testArray);

// [1, 2, 3, 4]
const b = Add({payload: 4})(testArray);

GetByIndices

Signature

/* arguments object */
({ 
    indices: number[],
    ignoreIndicesOutOfArray: boolean
})
/* returns */
(source: *[]) => []

Usage example

const { GetByIndices } = require("thesmo-calc-fields-array");

const testArray = [7, 8, 9];

// [7, 9]
const a = GetByIndices({
    ignoreIndicesOutOfArray: true, 
    indices: [0, 2, 7]
})(testArray);

// [7, 9, undefined]
const b = GetByIndices({
    ignoreIndicesOutOfArray: false, 
    indices: [0, 2, 7]
})(testArray);

// [9, 7]
const c = GetByIndices({
    ignoreIndicesOutOfArray: true, 
    indices: [2, 0]
})(testArray);

// [9, 9, 9, 9]
const d = GetByIndices({
    ignoreIndicesOutOfArray: true, 
    indices: [2, 2, 2, 2]
})(testArray);

GetLength

Signature

/* arguments object */
({
    errorValue?: number,
    skipUndefinedValues?: boolean,
    skipNullValues?: boolean
})
/* returns */
(source: *[]) => number

Usage example

const { GetLength } = require("thesmo-calc-fields-array");

const testArray = [7, undefined, null, null];
const notAnArray = 55;

// 4
const a = GetLength({})(testArray);
// 3
const b = GetLength({ skipUndefinedValues: true })(testArray);
// 2
const c = GetLength({ skipNullValues: true })(testArray);
// 1
const d = GetLength({ 
    skipNullValues: true,
    skipUndefinedValues: true
})(testArray);
// -1
const e = GetLength({})(notAnArray);
// -10
const f = GetLength({ errorValue: -10 })(notAnArray);

Has

Signature

/* arguments object */
({ 
    element: *,
    equalityCheck: (firstOperand: *, secondOperand: *) => boolean
})
/* returns */
(source: *[]) => boolean

Usage example

const { Has } = require("thesmo-calc-fields-array");
const { Primitives } = require("thesmo-compare-values")

const comparePrimitives = Primitives({shouldIgnoreTypes: false});

const testArray = [1,2,3];

// true
const a = Has({
    element: 1, 
    equalityCheck: comparePrimitives
})(testArray);

// false
const b = Has({
    element: 4, 
    equalityCheck: comparePrimitives
})(testArray);

Merge

Signature

/* arguments object */
({ 
    payload: *,
    equalityCheck: (firstOperand: *, secondOperand: *) => boolean,
    shouldReplaceRecordsWithNewOnes?: boolean
})
/* returns */
(source: *[]) => boolean

Usage example

const { Merge } = require("thesmo-calc-fields-array");
const { Primitives } = require("thesmo-compare-values")

const comparePrimitives = Primitives({shouldIgnoreTypes: false});

const testArray = [1, 2, 3, 4];

// [1, 2, 3, 4, 5]
const a = Merge({
    equalityCheck: comparePrimitives, 
    payload: [2, 5]
})(testArray);

// [1, 2, 3, 4]
const b = Merge({
    equalityCheck: comparePrimitives, 
    payload: 2
})(testArray);

// [1, 2, 3, 4, 5]
const c = Merge({
    equalityCheck: comparePrimitives, 
    payload: 5
})(testArray);

Remove

Signature

/* arguments object */
({ 
    element: *,
    equalityCheck: (firstOperand: *, secondOperand: *) => boolean
})
/* returns */
(source: *[]) => *[]

Usage example

const { Remove } = require("thesmo-calc-fields-array");
const { Primitives } = require("thesmo-compare-values")

const comparePrimitives = Primitives({shouldIgnoreTypes: false});

const testArrayOfDigits = [1, 2, 3];
const testArrayOfObjects = [{id: 1}, {id: 2}, {id: 3}];

// [1, 3]
const a = Remove({
    element: 2, 
    equalityCheck: comparePrimitives
})(testArrayOfDigits);

// [{id: 2}, {id: 3}]
const b = Remove({
    element: {id: 1}, 
    equalityCheck: (a, b) => comparePrimitives(a.id, b.id)
})(testArrayOfObjects);

RemoveMultiple

Signature

/* arguments object */
({ 
    elements: *[],
    equalityCheck: (firstOperand: *, secondOperand: *) => boolean
})
/* returns */
(source: *[]) => *[]

Usage example

const { RemoveMultiple } = require("thesmo-calc-fields-array");
const { Primitives } = require("thesmo-compare-values")

const comparePrimitives = Primitives({shouldIgnoreTypes: false});

const testArrayOfDigits = [1, 2, 3];
const testArrayOfObjects = [{id: 1}, {id: 2}, {id: 3}];

//[2]
const a = RemoveMultiple({
    elements: [1, 3], 
    equalityCheck: comparePrimitives
})(testArrayOfDigits);

//[{id: 3}]
const b = RemoveMultiple({
    elements: [{id: 1}, {id: 2}], 
    equalityCheck: (a, b) => comparePrimitives(a.id, b.id)
})(testArrayOfObjects);

SearchForIndices

Signature

/* arguments object */
({
    requiredIndicesCheck: (member: *, zeroBasedIndex: number) => boolean
})
/* returns */
(source: *[]) => number[]

Usage example

const { SearchForIndices } = require("thesmo-calc-fields-array");

const testArrayOfDigits = [1, 2, 3, 4, 5, 6];

//[0, 2, 4, 5]
const indices = SearchForIndices({
    requiredIndicesCheck: (member, zeroBasedIndex) => member % 2 == 1 || zeroBasedIndex > 3
})(testArrayOfDigits);

Toggle

Signature

/* arguments object */
({ 
    element: *,
    equalityCheck: (firstOperand: *, secondOperand: *) => boolean
})
/* returns */
(source: *[]) => *[]

Usage example

const { Toggle } = require("thesmo-calc-fields-array");
const { Primitives } = require("thesmo-compare-values")

const comparePrimitives = Primitives({shouldIgnoreTypes: false});

const testArrayOfDigits = [1, 2, 3];

const toggle1 = Toggle({element: 1, equalityCheck: comparePrimitives});

// [2, 3]
const arrayWithToggled1 = toggle1(testArrayOfDigits);

// [2, 3, 1]
const arrayWithToggled1Twice = toggle1(arrayWithToggled1);

UpdateWithPattern

Signature

/* arguments object */
({ 
    // keys of updating pattern are required indices,
    // values are according calculating fields
    updatingPattern: object
})
/* returns */
(source: *[]) => *[]

Usage example

const { UpdateWithPattern } = require("thesmo-calc-fields-array");
const { Create } = require("thesmo-redux-store");

const testStore = Create({
    defaultState: {
        fullArray: [1, 2, 3]
    }
});

testStore.To({
    at: "fullArray", 
    calcField: UpdateWithPattern({
        updatingPattern: {
            0: x => x + 15,
            1: x => x * 2,
            2: x => x - 100
        }
    })
});

// [16, 4, -97]
const fullArray = testStore.From({ at: "fullArray" })