Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@logdna/stdlib
Advanced tools
Standardized modular package exposing language constructs - free of business logic.
> npm install @logdna/stdlib
has
(obj: Object, property: String [, separator: String = '.']): Booleanget
(obj: Object, property: String [, separator: String = '.']): anyset
(obj: Object, property: String, value: any
[, separator: String = '.' ]): Objectfilter
(obj: Object, test: Function): Objecttypecast
(obj: Object [, depth: Number = 1000]): ObjecttypeOf
(element: any
): StringCallable
: ClassCommon array manipulation functions
toArray
(item: any
): ArrayConverts or wraps anything in an array. Set objects will be converted to arrays. A String will be treated as a CSV and parsed into an array. If an array cannont be coerced from the input value, it will be wrapped in an a single element array
Arguments:
item
- The element to convert to an array.returns result
Array: The resulting array for input coercion
const {array} = require('@logdna/stdlib')
array.toArray() // []
array.toArray(null) // []
array.toArray(1) // [1]
array.toArray('1,2, 4, 3') // [1, 2, 4, 3]
Iteration tools for complex and efficient iteration. Inspired by python's itertools
cycle
(items: Array): GeneratorIterates endlessly over a single array of items. The elements of the array can be of any type.
Arguments:
items
(Array) - An array to iterate overreturns Generator A generator object containing the passed in elements
const {iter} = require('@logdna/stdlib')
const numbers = iter.cycle([1, 2, 3])
numbers.next().value // 1
numbers.next().value // 2
numbers.next().value // 3
numbers.next().value // 1
numbers.next().value // 2
numbers.next().value // 3
Common json functions
parse
(input: String): ObjectSafe json parsing function that attempts to json parse a string. If it cannont, undefined will be returned
Arguments
input
(String) - a json string to parsereturns Object A fully parsed javascript object
const {json} = require('@logdna/stdlib')
json.parse('{"valid": "json"}') // {valid: 'json'}
json.parse('{"invalid" json') // undefined
Common object manipulation and intropsection functions
has
(obj: Object, property: String [, separator: String = '.']): BooleanDetermines if a specified key is a direct property of an object. This function is safe
to call on objects that do not inherit from Object.prototype, unlike attempting to call .hasOwnProperty
on input objects
Arguments
obj
(Object) - The object to introspectkey
(String) - The name of the property to locateseparator
(String) - Delimiter character
'.'
returns Boolean - True of the key is defined on the input object.
const {object} = require('@logdna/stdlib')
object.has({a: 1}, 'a') // true
object.has({}, 'test') // false
object.has(Object.create(null), 'test') // false
object.has({}, 'hasOwnProperty') // false
object.has({one: {two: {three: 3}}}, 'one-two-three', '-') // true
get
(obj: Object, property: String [, separator: String = '.']): anyReturns the value from an object at a specified object path.
Arguments
obj
(Object) - The object to introspectkey
(String) - The name of the property to localseparator
(String) - Delimiter character
'.'
returns any
- The value at the specified key. If no value is found, undefined
will be returned.
const {object} = require('@logdna/stdlib')
const obj = {one: {two: {three: 3}}}
const value = object.get(obj, 'one-two-three', '-') // 3
set
(obj: Object, property: String, value: any
[, separator: String = '.' ]): ObjectSets a property at the deepest level. Nested objects will be created if they do not exist. Returns the modified object. This will not work on complex Types like arrays or maps. Only POJOs
NOTE
: if you find your self wanting to set the value at a specific index of an array - you probably want an object.
Arguments
obj
(Object) - The object to introspectkey
(String) - The name of the property to localvalue
(any
) - The value to set at the specified pathseparator
(String) - Delimiter character
'.'
const {object} = require('@logdna/stdlib')
const obj = {one: {two: {three: 3}}}
const value = object.set(obj, 'four.five', 6)
// {one: { two: three: 3 }, four: {five: 6}}
filter
(obj: Object, test: Function): ObjectSimilar to array.filter, removes keys from an input object that do not pass the
test
function
NOTE
: This function returns a null
object - Object.create(null)
which does not
inherit from Object.prototype
Arguments
obj
(Object) - The object to introspecttest
(Function) - The function to be used to reject keys from the input object.
If this function returns a truthy
value, the key will be included in the final output. If falsey
it will be excludedconst {object} = require('@logdna/stdlib')
const obj = {one: { two: three: 3 } }
object.filter({two: 2, three: 3}, (key) => {
return key.match(/ee$/)
}) // {three: 3}
returns Object An object containing only the keys which passed the test function.
The return object will have a null
prototype.
typecast
(obj: Object [, depth: Number = 1000]): ObjectRecursively typecast string values of enumerable object properties,
using string.typecast()
Arguments
returns Object A new object with all string properties typecast.
const {object} = require('@logdna/stdlib')
const obj = {foo: '1', bar: 'null', baz: 'three', qux: {foo: '2'}}
const casted = typecast(obj)
// {foo: 1, bar: null, baz: 'three', qux: {foo: 2}}
const with_depth = typecast(obj, 0)
// {foo: 1, bar: null, baz: 'three', qux: {foo: '2'}}
camelcase
(text: String): StringCasts a string value to its camel case variant
Arguments
str
String - The string value to convertreturns String A camelcase version of the input string
const {string} = require('@logdna/stdlib')
string.camelcase('Hello George Washington') // helloGeorgeWashington
lowercase
(text: String): StringA safe alternative to String.prototype.toLowerCase(). This can be called non string inputs and they will be converted to string prior to lower casing.
Arguments
str
String - The string value to convertreturns String A lowercased version of the input string
const {string} = require('@logdna/stdlib')
string.lowercase('Hello George Washington') // hello george washington
string.lowercase({}) // [object object]
string.lowercase(null) // ''
string.lowercase(undefined) // ''
uppercase
(text: String): StringA safe alternative to String.prototype.toUpperCase(). This can be called non string inputs and they will be converted to string prior to upper casing.
Arguments
str
String - The string value to convertreturns String A uppercased version of the input string
const {string} = require('@logdna/stdlib')
string.uppercase('Hello George Washington') // HELLO GEORGE WASHINGTON
string.uppercase({}) // [OBJECT OBJECT]
string.uppercase(null) // ''
string.uppercase(undefined) // ''
slugify
(text: String [, separator: String = '-']): StringConverts a string to a url friendly format by replacing spaces and symbols with a known value and converting to lower case
Arguments
str
String - The string value to convertseparator
(String) - Delimiter character
'-'
returns String A slugified version of the input string
const {string} = require('@logdna/stdlib')
string.slugify('A fake Sentence') // a-fake-sentence
string.slugify('A fake Sentence', '::') // a::fake::sentence
typecast
(text: String): ObjectBest effort to cast a string to its native couter part where possible. Supported casts are booleans, numbers, null and undefined
Arguments
value
- The value to castreturns The native representation of the string input. If the value could not be cast, it will be returned as it was passed.
const {string} = require('@logdna/stdlib')
string.typecast('null') // null
string.typecast('true') // true
string.typecast('10.01') // 10.01
string.typecast({}) // {}
typeOf
(element: any
): StringA more accurate version of the javascript built-in function typeof
Arguments
input
- The input object to introspectreturns String A normalized string representation of the input object
const {typeOf} = require('@logdna/stdlib')
typeOf(new Date()) // 'date'
typeOf(/\w+/) // regexp
typeOf(() => {}) // function
typeOf(new Set()) // set
Callable
: ClassA class object whose instances are derived from Function and can be called.
When exteded, a Symbol function defined by Symbol.for('call')
will be executed
with any arguments that were passed
const {Callable} = require('@logdna/stdlib')
const __call__ = Symbol.for('call')
class Hello extends Callable {
constructor(opts) {
this.yell = !!opts.yell
}
[__call__](name) {
const output = `Hello, ${name}`
console.log(this.yell ? `${output.toUpperCase()}!` : output)
}
}
const screamAt = new Hello({yell: true})
screamAt('bill') // HELLO, BILL!
Thanks goes to these wonderful people (emoji key):
Darin Spivey 💻 👀 ⚠️ 📖 | Eric Satterwhite 💻 👀 ⚠️ 📖 | Jon Moses 📖 🐛 | Mike Del Tito 💻 👀 ⚠️ 📖 | Samir Musali 💻 | Wei Zou 💻 👀 ⚠️ 📖 |
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
Standardized module functions free of business logic
The npm package @logdna/stdlib receives a total of 13,082 weekly downloads. As such, @logdna/stdlib popularity was classified as popular.
We found that @logdna/stdlib demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.