A comprehensive, lightweight, and modern JavaScript utility library designed to bring Python-like simplicity to your daily coding tasks.
pythonizejs brings the simplicity of Python-like syntax and the power of functional programming to JavaScript. Whether you need robust math operations, string manipulation, array helpers, or type checking, this package has you covered.
- Zero Dependencies: Lightweight and fast.
- Tree Shakeable: Import only what you need.
- Python-like Syntax: Familiar functions like
println,range,len. - Functional: Helpers for arrays and collections.
- Type Safe: Robust type checking utilities.
Install the package via npm:
npm install pythonizejsThis package is an ES Module. You can import specific functions as needed to keep your bundle size small.
import { println, range, sum, isPalindrome } from 'pythonizejs';
// Python-like print
println("Hello, World!");
// Generate a range of numbers
const numbers = range(1, 10);
// Calculate sum
const total = sum(numbers);
// Check for palindrome
const isPal = isPalindrome("racecar"); // true| Function | Description | Example |
|---|---|---|
println(n) |
Prints output to the console (like Python's print). | println("Hello") |
len(n) |
Returns the length of a string or array safely. | len([1, 2, 3]) // 3 |
type(n) |
Returns the type of the variable. | type(123) // 'number' |
str(n) |
Converts a value to a string. | str(123) // "123" |
int(n) |
Parses a string to an integer. | int("12.5") // 12 |
input(msg) |
Prompts the user for input (Browser only). | const name = input("Name?") |
sleep(ms) |
Pauses execution for a given time (Promise-based). | await sleep(1000) |
now() |
Returns the current timestamp (Date.now()). | const ts = now() |
timer(fn) |
Measures the execution time of a function. | const ms = timer(() => doWork()) |
| Function | Description | Example |
|---|---|---|
square(n) |
Returns the square of a number. | square(4) // 16 |
sqrt(n) |
Returns the square root of a number. | sqrt(16) // 4 |
pow(a, b) |
Returns a raised to the power of b. |
pow(2, 3) // 8 |
abs(n) |
Returns the absolute value. | abs(-5) // 5 |
round(n) |
Rounds to the nearest integer. | round(4.6) // 5 |
floor(n) |
Rounds down to the nearest integer. | floor(4.9) // 4 |
ceil(n) |
Rounds up to the nearest integer. | ceil(4.1) // 5 |
clamp(n, min, max) |
Restricts a number within a range. | clamp(10, 0, 5) // 5 |
rand(start, end) |
Returns a random number between start and end. | rand(1, 10) |
| Function | Description | Example |
|---|---|---|
upper(str) |
Converts string to uppercase. | upper("hello") // "HELLO" |
lower(str) |
Converts string to lowercase. | lower("HELLO") // "hello" |
reverse(str) |
Reverses a string. | reverse("abc") // "cba" |
count(str, ch) |
Counts occurrences of a character. | count("hello", "l") // 2 |
isPalindrome(str) |
Checks if a string is a palindrome. | isPalindrome("madam") // true |
| Function | Description | Example |
|---|---|---|
sum(arr) |
Returns the sum of all elements. | sum([1, 2, 3]) // 6 |
avg(arr) |
Returns the average of all elements. | avg([2, 4, 6]) // 4 |
max(arr) |
Returns the maximum value in an array. | max([1, 5, 2]) // 5 |
min(arr) |
Returns the minimum value in an array. | min([1, 5, 2]) // 1 |
unique(arr) |
Removes duplicate values from an array. | unique([1, 1, 2]) // [1, 2] |
flatten(arr) |
Flattens a nested array recursively. | flatten([[1], [2]]) // [1, 2] |
chunk(arr, size) |
Splits an array into chunks of specific size. | chunk([1, 2, 3, 4], 2) // [[1, 2], [3, 4]] |
range(start, end, step) |
Generates an array of numbers. | range(0, 5) // [0, 1, 2, 3, 4] |
all(arr, fn) |
Checks if all elements satisfy a condition. | all([2, 4], n => n % 2 === 0) // true |
any(arr, fn) |
Checks if any element satisfies a condition. | any([1, 2], n => n % 2 === 0) // true |
| Function | Description | Example |
|---|---|---|
isArray(n) |
Checks if the value is an array. | isArray([]) // true |
isObject(n) |
Checks if the value is an object (and not null). | isObject({}) // true |
isNumber(n) |
Checks if the value is a valid number. | isNumber(123) // true |
isString(n) |
Checks if the value is a string. | isString("hi") // true |
isEmpty(n) |
Checks if a string, array, or object is empty. | isEmpty([]) // true |
| Function | Description | Example |
|---|---|---|
times(n, fn) |
Executes a function n times. |
times(3, i => println(i)) |
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by Puneeth Aditya