Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
The align-text npm package is used to align text in various ways, such as left, right, or center alignment. It provides a simple API to manipulate text alignment in strings, which can be useful for formatting console output, generating text-based reports, or creating command-line interfaces.
Center Alignment
This feature allows you to center-align a given string. The code sample demonstrates how to center-align the text 'Hello, World!'.
const align = require('align-text');
const result = align('Hello, World!', { align: 'center' });
console.log(result);
Left Alignment
This feature allows you to left-align a given string. The code sample demonstrates how to left-align the text 'Hello, World!'.
const align = require('align-text');
const result = align('Hello, World!', { align: 'left' });
console.log(result);
Right Alignment
This feature allows you to right-align a given string. The code sample demonstrates how to right-align the text 'Hello, World!'.
const align = require('align-text');
const result = align('Hello, World!', { align: 'right' });
console.log(result);
The cli-table package is used to create tables in the console with various alignment options for text within the table cells. It provides more advanced table formatting capabilities compared to align-text, including support for table headers, column widths, and custom styles.
The columnify package formats data into columns with customizable alignment options. It is particularly useful for displaying tabular data in the console. Compared to align-text, columnify offers more flexibility in handling multiple columns and complex data structures.
The string-width package calculates the visual width of a string, taking into account characters with different widths. While it does not directly align text, it can be used in conjunction with other packages to ensure proper alignment by accurately measuring string lengths. It complements align-text by providing precise width calculations.
Align the text in a string.
Follow this project's author, Jon Schlinkert, for updates on this project and others.
Install with npm:
$ npm install --save align-text
var align = require('align-text');
align(text, function_or_integer);
Params
text
can be a string or array. If a string is passed, a string will be returned. If an array is passed, an array will be returned.function|integer
: if an integer, the text will be indented by that amount. If a transform function is passed, it must return an object with an integer
property or an integer representing the amount of leading indentation to use as align
loops over each line.Example
align(text, 4);
Would align:
abc
abc
abc
To:
abc
abc
abc
The callback is used to determine the indentation of each line and gets the following params:
len
the length of the "current" linelongest
the length of the longest lineline
the current line (string) being alignedlines
the array of all linesThe callback may return:
indent
: {Number} the amount of indentation to use. Default is 0
when an object is returned.character
: {String} the character to use for indentation. Default is ''
(empty string) when an object is returned.prefix
: {String} leading characters to use at the beginning of each line. ''
(empty string) when an object is returned.Integer example:
// calculate half the difference between the length
// of the current line and the longest line
function centerAlign(len, longest, line, lines) {
return Math.floor((longest - len) / 2);
}
Object example:
function centerAlign(len, longest, line, lines) {
return {
character: '\t',
indent: Math.floor((longest - len) / 2),
prefix: '~ ',
}
}
Align text values in an array:
align([1, 2, 3, 100]);
//=> [' 1', ' 2', ' 3', '100']
Visit the example to see how this works.
Using the centerAlign
function from above:
align(text, centerAlign);
Would align this text:
Lorem ipsum dolor sit amet
consectetur adipiscin
elit, sed do eiusmod tempor incididun
ut labore et dolor
magna aliqua. Ut enim ad mini
veniam, quis
Resulting in this:
Lorem ipsum dolor sit amet,
consectetur adipiscing
elit, sed do eiusmod tempor incididunt
ut labore et dolore
magna aliqua. Ut enim ad minim
veniam, quis
Customize
If you wanted to add more padding on the left, just pass the number in the callback.
For example, to add 4 spaces before every line:
function centerAlign(len, longest, line, lines) {
return 4 + Math.floor((longest - len) / 2);
}
Would result in:
Lorem ipsum dolor sit amet,
consectetur adipiscing
elit, sed do eiusmod tempor incididunt
ut labore et dolore
magna aliqua. Ut enim ad minim
veniam, quis
align(text, function (len, max, line, lines) {
return {prefix: ' - '};
});
Would return:
- Lorem ipsum dolor sit amet,
- consectetur adipiscing
- elit, sed do eiusmod tempor incididunt
- ut labore et dolore
- magna aliqua. Ut enim ad minim
- veniam, quis
align(text, function (len, max, line, lines) {
return {
indent: Math.floor((max - len) / 2),
character: '~',
};
});
Would return
~~~~~Lorem ipsum dolor sit amet,
~~~~~~~~consectetur adipiscing
elit, sed do eiusmod tempor incididunt
~~~~~~~~~ut labore et dolore
~~~~magna aliqua. Ut enim ad minim
~~~~~~~~~~~~~veniam, quis
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Commits | Contributor |
---|---|
14 | jonschlinkert |
2 | shinnn |
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Jon Schlinkert
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on September 13, 2017.
FAQs
Align the text in a string.
The npm package align-text receives a total of 2,623,544 weekly downloads. As such, align-text popularity was classified as popular.
We found that align-text demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.