Python-inspired utilities for JavaScript.
Install pyjar from npm:
npm install pyjarThen require it in your JavaScript project:
const pyjar = require("pyjar");You can also destructure the functions you need:
const {
print,
len,
range,
enumerate,
zip,
sum,
min,
max,
sorted,
reversed,
abs,
any,
all,
keys,
values,
items,
get,
append,
extend,
pop,
isinstance,
startswith,
endswith
} = require("pyjar");A Python-style wrapper around JavaScript’s console.log().
print("Hello");
print("Value:", 42);Returns the length or size of a value.
Supports strings, arrays, Map, Set, and objects.
len([1, 2, 3]);
// 3len("hello");
len(new Set([1, 2, 3]));
len({ name: "Alice", age: 25 });
Passing null or undefined throws a TypeError.
Creates an array of numbers using Python-style range syntax.
range(5);
// [0, 1, 2, 3, 4]range(2, 5);
range(2, 10, 2);
range(5, 0, -1);
The step value cannot be 0.
Creates index-value pairs from an iterable.
enumerate(["apple", "banana", "orange"]);
// [
// [0, "apple"],
// [1, "banana"],
// [2, "orange"]
// ]A custom starting index can be provided:
enumerate(["apple", "banana"], 1);
// [
// [1, "apple"],
// [2, "banana"]
// ]Combines multiple iterables into arrays of corresponding values.
zip(
["Alice", "Bob"],
[20, 25]
);The result stops at the shortest iterable.
Calculates the sum of values in an iterable.
sum([1, 2, 3]);
// 6sum([1, 2, 3], 10);
The second argument specifies the starting value.
Find the smallest or largest value in an iterable.
min([3, 1, 2]);
// 1max([3, 1, 2]);
Returns a sorted copy of an iterable without modifying the original.
sorted([3, 1, 2]);
// [1, 2, 3]Use reverse to sort in descending order:
sorted([3, 1, 2], null, true);
// [3, 2, 1]A key function can be supplied for custom sorting:
const users = [
{ name: "Alice", age: 30 },
{ name: "Bob", age: 20 },
{ name: "Charlie", age: 25 }
];sorted(users, user ⇒ user.age);
Returns true if at least one value is truthy.
any([false, false, true]);
// trueA predicate can also be provided:
any([1, 2, 3], value => value > 2);
// trueAppends a value to an array.
const numbers = [1, 2];append(numbers, 3);
console.log(numbers);
The modified array is returned.
Adds multiple values to an array.
const numbers = [1, 2];extend(numbers, [3, 4]);
console.log(numbers);
Provides Python-style type checking for common JavaScript values.
isinstance(10, "int");
// trueisinstance(10.5, "float");
isinstance(10, "number");
isinstance("hello", "str");
isinstance(true, "bool");
isinstance([1, 2], "list");
isinstance({ name: "Alice" }, "dict");
isinstance(null, "null");
isinstance(undefined, "undefined");
Supported type names:
int float number str bool list dict null undefined
For other values, isinstance() uses JavaScript’s instanceof.
const {
print,
range,
enumerate,
sum,
sorted,
isinstance,
startswith
} = require("pyjar");const numbers = [5, 2, 8, 1, 3];
print("Numbers:", numbers); print("Range:", range(5)); print("Sum:", sum(numbers)); print("Sorted:", sorted(numbers));
for (const [index, value] of enumerate(numbers)) { print(index, value); }
print(isinstance(numbers, "list")); print(startswith("pyjar", "py"));
| Function | Description |
|---|---|
print() |
Print values to the console |
len() |
Get the length or size of a value |
range() |
Generate a sequence of numbers |
enumerate() |
Generate index-value pairs |
zip() |
Combine multiple iterables |
sum() |
Calculate the sum of an iterable |
min() |
Find the minimum value |
max() |
Find the maximum value |
sorted() |
Return a sorted copy of an iterable |
reversed() |
Return a reversed copy of an iterable |
abs() |
Return the absolute value |
any() |
Check whether any value is truthy or matches a predicate |
all() |
Check whether all values are truthy or match a predicate |
keys() |
Get the keys of an object |
values() |
Get the values of an object |
items() |
Get the entries of an object |
get() |
Get an object property with a default value |
append() |
Append a value to an array |
extend() |
Extend an array with multiple values |
pop() |
Remove and return an array value |
isinstance() |
Perform Python-style type checking |
startswith() |
Check whether a string starts with a prefix |
endswith() |
Check whether a string ends with a suffix |
The npm project currently contains the main library, package metadata, tests, and package archive. {"fallbackMarkdown":"(GitHub )","reference":{"matched_text":"","prefix":null,"start_idx":7794,"end_idx":7811,"safe_urls":["https://github.com/cloudberrypitech/pyjar"],"refs":[],"alt":"(GitHub )","prompt_text":null,"type":"grouped_webpages","items":[{"title":"GitHub - cloudberrypitech/pyjar · GitHub","url":"https://github.com/cloudberrypitech/pyjar","attribution":"GitHub","pub_date":null,"snippet":null,"thumbnail_url":"https://images.openai.com/static-rsc-1/9Ug32kFLNqnxErwTz82uzn6cjUWDSbOn9OnKqLVbz2aLElHeVgTL1HazSnOtRU46x5zOq1XM3hbwwNJte0z6OKtVKuxsl_7d4nRsDE8DsUS8TAR-9Qe7u0yWd7p_vIhVb7-nKgF4hUis1at3qz9wtQE6JMDNCVTfaq909WDiYacyL7Jc6IcsUMNON3W-jQJc","attribution_segments":null,"supporting_websites":,"refs":[{"turn_index":0,"ref_type":"view","ref_index":0}],"hue":null,"attributions":null}],"fallback_items":null,"style":null,"status":"done","error":null},"showLoginRequiredCard":false}
pyjar/
├── index.js
├── package.json
├── test.js
├── pyjar-1.0.0.tgz
└── .github/
└── workflows/After installing the package, import the utilities you need:
const { print, len, range } = require("pyjar");print(len([1, 2, 3])); print(range(5));
pyjar is designed to make familiar Python-style operations available while working in JavaScript. :::{"fallbackMarkdown":"","reference":{"matched_text":" ","prefix":null,"start_idx":8350,"end_idx":8350,"safe_urls":[],"refs":[],"alt":"","prompt_text":null,"type":"sources_footnote","sources":[{"title":"GitHub - cloudberrypitech/pyjar · GitHub","url":"https://github.com/cloudberrypitech/pyjar","attribution":"GitHub"}],"has_images":false},"showLoginRequiredCard":false}