Skip to content

Python-like utility functions for JavaScript developers pythonizejs brings familiar Python-style helper functions into JavaScript, making it easier and more intuitive to practice and write JS—especially for beginners and Python developers transitioning to JavaScript.

Notifications You must be signed in to change notification settings

mugiwaraluffy56/pythonizejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pythonizejs

npm version License Downloads Bundle Size

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.

✨ Features

  • 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.

📋 Table of Contents

📦 Installation

Install the package via npm:

npm install pythonizejs

🚀 Usage

This 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

📚 API Reference

🔹 Core Utilities

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())

🔢 Math Helpers

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)

📝 String Manipulation

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

📊 Array & Collection Utilities

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

✅ Type Checking

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

🔄 Control Flow

Function Description Example
times(n, fn) Executes a function n times. times(3, i => println(i))

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with ❤️ by Puneeth Aditya

About

Python-like utility functions for JavaScript developers pythonizejs brings familiar Python-style helper functions into JavaScript, making it easier and more intuitive to practice and write JS—especially for beginners and Python developers transitioning to JavaScript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published