buxlabs / pure-utilities Goto Github PK
View Code? Open in Web Editor NEWA set of pure utility methods.
License: MIT License
A set of pure utility methods.
License: MIT License
pad: {
parameters: ['string'],
description: ''
}
It would be nice to add a utility to sort objects by key, something like:
const { isPlainObject } = require('pure-conditions')
const recursiveSort = (object) => {
const newObject = {}
Object.keys(object).sort().forEach(key => {
const value = object[key]
if (isPlainObject(value)) {
newObject[key] = recursiveSort(value)
} else {
newObject[key] = value
}
})
return newObject
}
module.exports = {
recursiveSort
}
recsort
method to object/recsort.js
https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key
e.g. 1024 -> 1 KiB
e.g. something like
function hash () {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid () {
return hash() + hash() + '-' + hash() + '-' + hash() + '-' + hash() + '-' + hash() + hash() + hash()
}
hours => returns the hour part from the date
minutes => returns the minutes from the date
seconds => returns the seconds from the date
occurences
method in collection.js
occurences(["foo", "foo", "bar"], "foo")
returns 2)occurences("foo foo bar", "foo")
returns 2)here's a naive implementation, there's probably a better solution
return Array.from(new Set(array.map(item => JSON.stringify(item)))).map(JSON.parse)
partition
method to array.js
https://lodash.com/docs/4.17.15#partition
https://stackoverflow.com/questions/11345296/partitioning-in-javascript
As a developer, I'd like to import a function per file so that the bundler I use has less work to do.
Sometimes we need to define the size of file in bytes, but it is hard to read how many kilobytes or megabytes is has the the file without using calculators or comments.
// max file size is 400 kB
const MAX_FILE_SIZE = 409600
const MAX_FILE_SIZE = prettysize('400KB')
So we could pass to the method string with specific unit for example '10KB' and it would be converted to bytes.
Maybe the better idea is passing to the method a configuration object.
It should join an array elements with comma , separator. The second parameter should be spacing (default set to 0)
"100,01" -> 100.01
"100 000,01" -> 100000.01
"100000.01" -> 100000.01
"100000.01" -> 100000.01
"100,000.01" -> 100000.01
In the README, we're stating that the methods are side effect free. Unfortunately it's not true in all of the cases yet.
function deepclone (object) {
return JSON.parse(JSON.stringify(object))
}
deepclone
fn would throw if you'd pass undefined
or when the parsed object has circular references.
We probably need to add a default:
function deepclone (object = {}) {}
and wrap the fn body with try catch. The simplest thing to do is probably just return an empty object if there's an exception.
By definition, all functions should have no side effects. It means that we should never throw.
append/prepend/methods
mean
method to math.js
object -> recsort.js
It doesn't seem to work when !
is used. Probably doesn't work for other special characters too.
occurences("!!oo", "!")
occurences("!!foo", "!")
occurences("!foo bar", "foo")
Newest ava
requires unique test names, so they should be adjusted as a part of this task.
npm test
, npm run lint
npm run coverage
IssueHunt has been backed by the following sponsors. Become a sponsor
When the day number is smaller than 10, the format
method should prepend the number with the zero
htmlstrip
method does not have a risky regexp anymoreWe're breaking the pure
rule in many cases, e.g. in the simple method:
function first (array) {
return array[0]
}
the following code would throw when there's no array, or something else was passed.
array.js
, collection.js
etc.undefined
case (which is probably the most common one)Remove ava/nyc to pass the npm audit
union
method to array.js
intersection, duplicates, symdifference, difference
https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript/33034768#33034768
es6-to-amd
We'd like to have a utility method for regrouping nested arrays.
array/zip.js
We can add a new helper which may be helpful in some situations.
For example we sometimes do some operations on collections and later when we want to filter null values we need to use e.g. filter.Boolean()
compactmap should make transformations on elements but omits undefined and null values.
We can add also a strict flag or something similar to remove all falsy values
https://www.avanderlee.com/swift/compactmap-flatmap-differences-explained/
https://www.npmjs.com/package/compact-map
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.