Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

jeremyaboyd/linq.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

20 Commits

Repository files navigation

linq.js

A simple JS library that mimics some of the C# LINQ Extension. Most of the extensions are just wrappers to already existing Array prototype method.

Using this library, you are able to filter, sort, and map as you would in C#:

var data = users.where( u => u.isActive )
 .orderBy( "-lastLogin" )
		.skip( page * pageSize )
		.take( pageSize )
		.select( u => {
		 return {
		 "User Name": u.userName,
			"Last Login": new Date( u.lastLogin ).toLocaleString(),
			"Created On": new Date( u.createdOn ).toLocaleString()
		 }
		});

Documentation

Setup

var arrToSort = [
	{ first: 1, second: "a", third: 0, fourth: [ 1, 2, 3 ] },
	{ first: 1, second: "b", third: 0 },
	{ first: 2, second: "a", third: 0, fourth: [ 1, 2, 3 ] },
	{ first: 3, second: "b", third: 1 },
	{ first: 3, second: "b", third: 0 },
];

.any(predicate)

Returns a boolean based on whether the predicate returns a truthy value for any element.

arrToSort.any( a => a.first === 2 );
// expected outcome: true
arrToSort.any( a => a.first === 12 );
// expected outcome: false

.all(predicate)

Returns a boolean based on whether the predicate returns a truthy value for every element.

arrToSort.all( a => a.first );
// expected outcome: true
arrToSort.all( a => a.first === 1 );
// expected outcome: false

.where(predicate)

Filters the array based on the predicate function provided.

arrToSort.where( a => a.first === 2 );
// expected outcome: [{ first: 2, second: "a", third: 0 }]

.skip(count)

Skips count elements in the array.

arrToSort.skip( 4 );
// expected outcome: [{ first: 3, second: "b", third: 0 }]

.take(count)

Takes the first count elements int he array

arrToSort.take( 1 );
// expected outcome: [{ first: 1, second: "a", third: 0 }]

.orderBy(properties)

Orders array based on element's properties in ascending order unless denoted by a minus/subtract symbol.

arrToSort.orderBy( "first", "-second", "third" );
/* expected outcome: [
	{ first: 1, second: "b", third: 0, fourth: [ 1, 2, 3 ] },
	{ first: 1, second: "a", third: 0 },
	{ first: 2, second: "a", third: 0, fourth: [ 1, 2, 3 ] },
	{ first: 3, second: "b", third: 0 },
	{ first: 3, second: "b", third: 1 },
]*/

.select(func)

Returns array with elements based on the output of func

arrToSort.select( a => a.first );
// expected outcome: [ 1, 1, 2, 3, 3 ]

.selectMany(func)

Returns a flattened array with elements based on the output of func

arrToSort.selectMany( a => a.fourth );
// expected outcome: [ 1, 2, 3, undefined, 1, 2, 3, undefined, undefined, undefined ]
arrToSort.where( a => a.fourth ).selectMany( a => a.fourth );
// expected outcome: [ 1, 2, 3, 1, 2, 3 ]

.first([predicate]) and .firstOrDefault([predicate])

Will return the first element in the array. Default will return null if 0 elements exist, other wise exceptions are thrown if there are no elements in the array.

If the optional predicate parameter is provided, will execute a .where() on the array first.

arrToSort.first();
// expected outcome: { first: 1, second: "a", third: 0 }
[].first();
// expected outcome: Sequence contains no elements
[].firstOrDefault();
// expected outcome: null

.single([predicate]) and .singleOrDefault([predicate])

Will return the ONLY element in the array. Default will return null if 0 elements exist, other wise exceptions are thrown if not exactly 1 element exists in the array.

If the optional predicate parameter is provided, will execute a .where() on the array first.

arrToSort.take( 1 ).single();
// expected outcome: { first: 1, second: "a", third: 0 }
arrToSort.single();
// expected outcome: More than one element exists in sequence
[].single();
// expected outcome: Sequence contains no elements.
[].singleOrDefault();
// expected outcome: null

About

A simple JS library that mimics some of the C# LINQ Extensions.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

AltStyle によって変換されたページ (->オリジナル) /