Pure
Annotation
An annotation which is shared among all Saber methods. Enforces developer to make a method pure. Other than this, it has nothing but an empty body. Since the library is functional an annotation like that is crucial.
Pure Annotation
1
2
3
4
5
class NativeClass {

@Pure()
nativeMethod() {}
}
Important Note
PureMethod annotation is used in every Saber method.
SaberCore
className string
returnsSaberCore
Implements IHash and IStringIndex
Creates a new SaberCore. When a class implements IHashit contains a uidproperty. Implementing IStringIndex interface is merely for object indexing in TypeScript. SaberCore is a class which every Saber class should extend. It also has a readonly value for holding value type in a Saber class. But it cannot write the value in the constructor. This property is only defined to compel developer.
Class Initialisation
1
2
3
4
// You can't import SaberCore

const saberCore = new SaberCore('NativeObject')
console.log(saberCore)
Output
SaberCore {
className: "NativeObject",
uid: 323920221566,
value: "Cannot assign any value to SaberCore."
}
Important Note
Every Saber class extends this core class.
SaberObject
Extends SaberCore
A class that encapsulates a JavaScript Object with enhanced immutable methods.
create
value SaberObject | object | any
returnsSaberObject
static
Creates a new SaberObject. Designed as a static factory method. I totally recommend using this over normal initialisation.
Creating A Tree Object
1
2
3
4
import { SaberObject } from '@berakocc/saber'

const tree = SaberObject.create({ fruit: 'apple' })
console.log(tree)
Output
t {
className: "SaberObject",
uid: 201606621055,
value: Object {
fruit: "apple"
}
}
modify
o object
objectFunction ObjectFunction
propertyChain? string[ ]
returnsobject
static
Modifies an object without mutating it. Then returns the new object. When propertyChain is not given it, function mutates the property at the root level. This is a utility function for the rest of the class. ObjectFunction is a function which uses bound object asthis and returns an object
Manufacturing a Mercedes AMG
1
2
3
4
5
6
7
8
9
10
import { SaberObject } from '@berakocc/saber'

const car = { engine: 'v8' }
const carWithModel = SaberObject.modify(car, function() {
return {
...this,
model: 'Mercedes AMG'
}
})
console.log(carWithModel)
Output
Object {
engine: "v8",
model: "Mercedes AMG"
}
Important Note
Never use ObjectFunction as an arrow function.
add<T>
property string
value T
propertyChain? string[ ]
returnsSaberObject
generic
Adds or updates the given property with the given value.
Creating a robot that can speak
1
2
3
4
5
6
7
8
import { SaberObject } from '@berakocc/saber'

const robot = SaberObject.create();
const enhancedRobot = robot.add('speak', () => {
console.log('Greetings, human!')
})
enhancedRobot.value.speak()
Output
"Greetings, human!"
Important Note
Generics are not supported in Vanilla JS.
remove
property string
propertyChain? string[ ]
returnsSaberObject
Removes the given property.
Creating a robot that can speak
1
2
3
4
5
6
7
8
import { SaberObject } from '@berakocc/saber

const person = SaberObject.create({
name: 'John Doe',
age: 23
})
const personWithoutAge = person.remove('age')
console.log(personWithoutAge.value)
Output
Object {
name: "John Doe"
}
copy
Copies the object value and returns a new SaberObject.
Copying a product
1
2
3
4
5
6
7
8
9
import { SaberObject } from '@berakocc/saber

const product = SaberObject.create({
weight: 200,
price: '150€'
})
const copiedProduct = product.copy()
console.log(copiedProduct.value)
console.log(copiedProduct.uid !== product.uid)
Output
Object {
price: "150€",
weight: 200
}

true
merge
s SaberObject
returnsSaberObject
Merges two SaberObjects.
Creating a robot that can speak
1
2
3
4
5
6
import { SaberObject } from '@berakocc/saber

const object1 = SaberObject.create({ id: 18294 })
const object2 = SaberObject.create({ value: 2854 })
const mergedObject = object1.merge(object2)
console.log(mergedObject.value)
Output
Object {
id: 18294,
value: 2854
}
SaberArray<T>
Extends SaberCore
generic
A class that encapsulates a JavaScript Array with enhanced immutable methods.
create<T>
value? T | T[ ] | SaberArray<T>
returnsSaberArray<T>
generic
static
Creates a new SaberArray.Designed as a static factory method. I totally recommend using this over normal initialisation.
Creating square number set
1
2
3
4
5
import { SaberArray } from '@berakocc/saber'

const squareNumbers = SaberArray.create([1,4,9])
console.log(squareNumbers.value)
Output
[1, 4, 9]
append
v T
returnsSaberArray<T>
Appends the given value.
Adding hex color to the color set
1
2
3
4
5
import { SaberArray } from '@berakocc/saber

const colorSet = SaberArray.create(['red', 'green'])
const updatedColorSet = colorSet.append('blue')
console.log(updatedColorSet.value)
Output
["red", "green", "blue"]
prepend
v T
returnsSaberArray<T>
Prepends the given value.
Prepending a new engine
1
2
3
4
5
import { SaberArray } from '@berakocc/saber

const engines = SaberArray.create(['v10', 'v12'])
const updatedEngines = engines.prepend('v8')
console.log(updatedEngines.value)
Output
["v8", "v10", "v12"]
copy
returnsSaberArray<T>
Copies the current value and returns a new SaberArray.
Copying sentences
1
2
3
4
5
6
import { SaberArray } from '@berakocc/saber

const texts = SaberArray.create(['Hello World!', 'Lorem ipsum'])
const copiedTexts = texts.copy()
console.log(copiedTexts.value)
console.log(texts.uid !== copiedTexts.uid)
Output
["Hello World!", "Lorem ipsum"]

true
merge
saberArray SaberArray<T>
returnsSaberArray<T>
Merges two SaberArray.
Merging two dictionary
1
2
3
4
5
6
import { SaberArray } from '@berakocc/saber

const dictionary1 = SaberArray.create(['tree', 'mountain'])
const dictionary2 = SaberArray.create(['sun', 'planet'])
const mergedDictionary = dictionary1.merge(dictionary2)
console.log(mergedDictionary.value)
Output
["tree", "mountain", "sun", "planet"]
insert
v T
i number
returnsSaberArray<T>
Updates the value at the given index.
Inserting a new element
1
2
3
4
5
import { SaberArray } from '@berakocc/saber

const elements = SaberArray.create(['Au', 'Li'])
const updatedElements = elements.insert('Bi', 1)
console.log(updatedElements.value)
Output
["Au", "Bi", "Li"]
update
v T
indexer T | number
isIndexerBasedOnValue boolean = false
returnsSaberArray<T>
Updates array value with the given index and value. indexer can be either a value of array or an index. However there can be a confusion when the array value is number. Therefore you have to use a third argument true when you want to use indexer as a value indexer.
Updating a broken tool
1
2
3
4
5
import { SaberArray } from '@berakocc/saber

const tools = SaberArray.create(['hammer', 'ladder', 'nuts'])
const mendedTools = tools.update('bolts', 'nuts', true)
console.log(mendedTools.value)
Output
["hammer", "ladder", "bolts"]
remove
v T
returnsSaberArray<T>
Removes the given value from the array. If value is duplicate removes the duplicates.
Removing A letters
1
2
3
4
5
import { SaberArray } from '@berakocc/saber

const letterSet = SaberArray.create(['A', 'B', 'C', 'A'])
const letterSetWithoutA = letterSet.remove('A')
console.log(letterSetWithoutA.value)
Output
["B", "C"]