easyjs-empty
v0.1.1
Published
An empty core of @cooljs123/easyjs for plugins (e.g., easyjs-maths, easyjs-time, easyjs-random).
Maintainers
Readme
EasyJS Empty
Minimal EasyJS core with only a plugin system. Nothing is loaded by default.
Install
npm i easyjs-emptyPlugins
A plugin is a function that receives the EasyJS instance and adds methods.
const reversePlugin = (easy) => {
easy.reverse = (str) => String(str).split("").reverse().join("");
};
easy.use(reversePlugin);
console.log(easy.reverse("hello")); // "olleh"Official Plugins (Examples)
Install
npm i easyjs-maths easyjs-time easyjs-random easyjs-date easyjs-json easyjs-collectionsEach plugin has:
- basic set loaded by
easy.use(plugin) - advanced set loaded by
easy.use(plugin.advanced)
easyjs-maths
easy.use(maths) loads basic. easy.use(maths.advanced) loads basic + advanced + random.
Basic
sqrt
// Takes a number and returns square root.
easy.sqrt(9) // 3pow
// Takes base and exponent and returns power.
easy.pow(2, 3) // 8floor
// Takes a number and returns floor.
easy.floor(3.9) // 3ceil
// Takes a number and returns ceil.
easy.ceil(3.1) // 4round
// Takes a number and returns rounded.
easy.round(3.5) // 4abs
// Takes a number and returns absolute value.
easy.abs(-5) // 5min
// Takes numbers and returns smallest.
easy.min(3, 1, 9) // 1max
// Takes numbers and returns largest.
easy.max(3, 1, 9) // 9Advanced
factorial
// Takes a non-negative integer and returns factorial.
easy.factorial(5) // 120gcd
// Takes two integers and returns greatest common divisor.
easy.gcd(12, 18) // 6lcm
// Takes two integers and returns least common multiple.
easy.lcm(4, 6) // 12lerp
// Takes a, b, t and returns linear interpolation.
easy.lerp(0, 10, 0.5) // 5sin
// Takes radians and returns sine.
easy.sin(Math.PI / 2) // 1cos
// Takes radians and returns cosine.
easy.cos(0) // 1tan
// Takes radians and returns tangent.
easy.tan(0) // 0asin
// Takes value and returns arcsin (radians).
easy.asin(1) // 1.5707963267948966acos
// Takes value and returns arccos (radians).
easy.acos(1) // 0atan
// Takes value and returns arctan (radians).
easy.atan(1) // 0.7853981633974483log
// Takes value (and optional base) and returns log.
easy.log(8, 2) // 3log10
// Takes value and returns base-10 log.
easy.log10(100) // 2pi
// Constant pi.
easy.pi // 3.141592653589793e
// Constant e.
easy.e // 2.718281828459045Random (also in advanced)
random
// Returns random float 0..1.
easy.random() // randomrandint
// Takes min/max and returns random integer.
easy.randint(1, 6) // 1..6choice
// Takes array and returns random element.
easy.choice(["a", "b"]) // "a" or "b"shuffle
// Takes array and returns shuffled copy.
easy.shuffle([1,2,3]) // random orderuniform
// Takes min/max and returns random float.
easy.uniform(1, 2) // 1..2sample
// Takes array and n, returns n random elements.
easy.sample([1,2,3,4], 2) // random 2easyjs-time
easy.use(time) loads basic. easy.use(time.advanced) loads basic + advanced.
Basic
now
// Returns current Date.
easy.now() // Datetime
// Returns seconds since epoch.
easy.time() // numbersleep
// Takes seconds and returns a Promise that resolves after delay.
await easy.sleep(1) // Promisectime
// Takes seconds (optional) and returns Date string.
easy.ctime(0) // "Thu Jan 01 1970 ..."localtime
// Takes seconds (optional) and returns Date.
easy.localtime(0) // Dategmtime
// Takes seconds (optional) and returns Date.
easy.gmtime(0) // DateAdvanced
timeMs
// Returns milliseconds since epoch.
easy.timeMs() // numbertimeNs
// Returns nanoseconds (best effort).
easy.timeNs() // bigint or numbermonotonic
// Returns monotonic clock in ms.
easy.monotonic() // numberperfCounter
// Returns high-resolution timer in ms.
easy.perfCounter() // numberperfCounterNs
// Returns high-resolution timer in ns (best effort).
easy.perfCounterNs() // bigint or numbersleepMs
// Takes ms and returns Promise after delay.
await easy.sleepMs(50) // Promisetimer
// Returns a function that gives elapsed ms.
const t = easy.timer();
t(); // numberelapsed
// Takes start ms and returns elapsed.
easy.elapsed(Date.now() - 1000) // ~1000format
// Formats a Date with pattern.
easy.format(new Date(0), "YYYY-MM-DD") // "1970-01-01"strftime
// Formats a Date with % tokens.
easy.strftime("%Y-%m-%d", new Date(0)) // "1970-01-01"timezoneOffsetMinutes
// Returns local timezone offset in minutes.
easy.timezoneOffsetMinutes() // numbereasyjs-random
easy.use(random) loads basic. easy.use(random.advanced) loads basic + advanced.
Basic
random
// Returns random float 0..1.
easy.random() // randomrandint
// Takes min/max and returns random integer.
easy.randint(1, 6) // 1..6choice
// Takes array and returns random element.
easy.choice(["a", "b"]) // "a" or "b"shuffle
// Takes array and returns shuffled copy.
easy.shuffle([1,2,3]) // random orderAdvanced
uniform
// Takes min/max and returns random float.
easy.uniform(1, 2) // 1..2sample
// Takes array and n, returns n random elements.
easy.sample([1,2,3,4], 2) // random 2chance
// Takes probability 0..1 and returns true/false.
easy.chance(0.25) // true/falseweightedChoice
// Takes items and weights and returns one item.
easy.weightedChoice(["a","b"],[1,3]) // "b" more ofteneasyjs-date
easy.use(date) loads basic. easy.use(date.advanced) loads basic + advanced.
Basic
today
// Returns current Date.
easy.today() // Datedate
// Returns current Date.
easy.date() // Dateyear
// Takes Date and returns year.
easy.year(new Date(0)) // 1970month
// Takes Date and returns month (1-12).
easy.month(new Date(0)) // 1day
// Takes Date and returns day of month.
easy.day(new Date(0)) // 1weekday
// Takes Date and returns day of week (0-6).
easy.weekday(new Date(0)) // 4toISO
// Takes Date and returns ISO string.
easy.toISO(new Date(0)) // "1970-01-01T00:00:00.000Z"parseDate
// Takes input and returns Date.
easy.parseDate("2020-01-01") // DateAdvanced
addDays
// Takes Date and number of days, returns new Date.
easy.addDays("2020-01-01", 1) // DateaddMonths
// Takes Date and number of months, returns new Date.
easy.addMonths("2020-01-01", 1) // DateaddYears
// Takes Date and number of years, returns new Date.
easy.addYears("2020-01-01", 1) // DatestartOfDay
// Takes Date and returns start of day.
easy.startOfDay("2020-01-01") // DateendOfDay
// Takes Date and returns end of day.
easy.endOfDay("2020-01-01") // DatediffDays
// Takes two dates and returns day difference.
easy.diffDays("2020-01-01", "2020-01-05") // 4isLeapYear
// Takes year and returns true if leap year.
easy.isLeapYear(2024) // trueformatDate
// Formats a Date with pattern.
easy.formatDate(new Date(0), "YYYY-MM-DD") // "1970-01-01"easyjs-json
easy.use(json) loads basic. easy.use(json.advanced) loads basic + advanced.
Basic
parse
// Takes JSON string and returns object.
easy.parse('{"a":1}') // { a: 1 }stringify
// Takes value and returns JSON string.
easy.stringify({ a: 1 }) // "{\"a\":1}"clone
// Takes JSON-serializable value and deep clones.
easy.clone({ a: 1 }) // { a: 1 }Advanced
safeParse
// Takes JSON string and fallback, returns object or fallback.
easy.safeParse("{bad}", null) // nullsafeStringify
// Takes value and returns JSON string or null.
easy.safeStringify({ a: 1 }) // "{\"a\":1}"pretty
// Takes value and returns pretty JSON string.
easy.pretty({ a: 1 }) // "{\n \"a\": 1\n}"compact
// Takes value and returns compact JSON string.
easy.compact({ a: 1 }) // "{\"a\":1}"isValid
// Takes JSON string and returns true/false.
easy.isValid('{"a":1}') // trueeasyjs-collections
easy.use(collections) loads basic. easy.use(collections.advanced) loads basic + advanced.
Basic
size
// Takes collection and returns size.
easy.size([1,2,3]) // 3keys
// Takes object and returns keys.
easy.keys({ a: 1 }) // ["a"]values
// Takes object and returns values.
easy.values({ a: 1 }) // [1]entries
// Takes object and returns entries.
easy.entries({ a: 1 }) // [["a", 1]]first
// Takes array and returns first.
easy.first([1,2,3]) // 1last
// Takes array and returns last.
easy.last([1,2,3]) // 3chunk
// Takes array and size and returns chunks.
easy.chunk([1,2,3,4], 2) // [[1,2],[3,4]]Advanced
uniq
// Takes array and removes duplicates.
easy.uniq([1,2,2,3]) // [1,2,3]flatten
// Takes array and flattens one level.
easy.flatten([1,[2,3]]) // [1,2,3]groupBy
// Takes array and function and groups items.
easy.groupBy([1,2,3,4], x => x % 2) // { 0:[2,4], 1:[1,3] }sortBy
// Takes array and function and sorts by key.
easy.sortBy([3,1,2], x => x) // [1,2,3]pick
// Takes object and keys and returns subset.
easy.pick({ a:1,b:2 }, ["a"]) // { a:1 }omit
// Takes object and keys and omits them.
easy.omit({ a:1,b:2 }, ["b"]) // { a:1 }Usage pattern
const easy = require("easyjs-empty");
const maths = require("easyjs-maths");
const time = require("easyjs-time");
const random = require("easyjs-random");
const date = require("easyjs-date");
const json = require("easyjs-json");
const collections = require("easyjs-collections");
// basic sets
easy.use(maths);
easy.use(time);
easy.use(random);
easy.use(date);
easy.use(json);
easy.use(collections);
// advanced sets
easy.use(maths.advanced);
easy.use(time.advanced);
easy.use(random.advanced);
easy.use(date.advanced);
easy.use(json.advanced);
easy.use(collections.advanced);