📜 ⬆️ ⬇️

Sugar - sweetened javascript

What is Sugar ?
This is a javascript library for working with embedded javascript objects, which:

As an example of use, the author cites the following code:
getLatestTweets( function (t) {
var users = t.map( 'user' ).unique();
var total = users.sum( 'statuses_count' ).format();
var top = users.max( 'followers_count' ).first();
var count = top.followers_count.format();
var since = Date.create(top.created_at);
return users.length + ' users with a total of ' + total + ' tweets.\n' +
top.screen_name + ' is the top with ' + count + ' followers\n' +
'and started tweeting ' + since.relative() + '.' ;
});


Isn't it true, the code reads almost like English text.
The most impatient can explore the functions provided by this link , and the rest will receive a brief description under the cat.

Arrays


Sugar fixes support for standard Javascript 1.6 methods, such as indexOf , forEach , reduce and others. The methods are refined to accept argument types that standard functions do not support: nested objects, regular expressions, strings. If possible, native browser methods will be used. Full list of methods .
[ 'a' , 'b' , 'c' ].indexOf( 'c' ); > 2
[{ foo: 'bar' }, { moo: 'car' }].indexOf({ moo: 'car' }); > 1
[ 'rocksteady' , 'and' , 'bebop' ].map( 'length' ); > [10,3,5]
[ 'rocksteady' , 'and' , 'bebop' ].sortBy( 'length' ); > [ "and" , "bebop" , "rocksteady" ]
[ 'rocksteady' , 'and' , 'bebop' ].findAll(/o/); > [ "rocksteady" , "bebop" ]
[ 'rocksteady' , 'and' , 'bebop' ].first(1); > [ "rocksteady" ]
[ 'rocksteady' , 'and' , 'bebop' ].from(1); > [ "and" , "bebop" ]
[ 'three' , 'two' , 'one' ].groupBy( 'length' ); > { "5" :[ "three" ], "3" :[ "two" , "one" ]}
[1,65,2,77,34].average(); > 35.8
[1,2].add([2,3]); > [1,2,2,3]
[1,2].subtract([2,3]); > [1]
[1,2].intersect([2,3]); > [2]
[1,2].union([2,3]); > [1,2,3]


Strings


Sugar corrects cross-browser differences when using split with regular expressions. Language support is improved by adding methods that support Unicode and Chinese, Japanese, Greek, Hebrew. Methods such as each , words , lines , paragraphs were added for flexible text handling. Full list of methods .
'abcdefgh' .split(/[bdf]/); > [ "a" , "c" , "e" , "gh" ]
'こんにちは' .hasHiragana(); > true
'환영 합니다' .hasHangul(); > true
' !' .hasCyrillic(); > true
'welcome' .pad(1, ' ' ).pad(3, '-' ); > "--- welcome ---"
'hut!' .repeat(3); > "hut!hut!hut!"
'off with her head!' .words(); > [ "off" , "with" , "her" , "head!" ]
'off with her head!' .each(/he.+?\b/g); > [ "her" , "head" ]
'off with her head!' .startsWith(/[az]ff/); > true
'off with her head!' .first(3); > "off"
'off with her head!' .from(3); > " with her head!"

')

Numbers


The main methods of the Math object are available in Sugar as shortcuts. Additional buns are rounding with the necessary accuracy, alignment of numbers and their formatting. Sugar also has methods that link numbers in dates for easy conversion of one to the other. Full list of methods .
(125.425).round(2); > 125.43
(125.425).round(-2); > 100
(4235000).format(); > "4,235,000"
(50).pad(5); > "00050"
(23).ordinalize(); > "23rd"
(5).upto(10); > [5,6,7,8,9,10]
(5).daysAfter( 'Wednesday' ); > "Monday, August 8, 2011 00:00"
(5).yearsBefore( '2001' ); > "Monday, January 1, 1996 00:00"
(5).times( function () {
/* Run 5 times */
})



Dates


Sugar adds the Date.create method to dates, which understands dates in various formats (unfortunately dd.mm.yyyy does not understand) - text, relative, absolute, timestamps. Dates can be output in various formats using a simple syntax. Using the is method, which understands all date entry formats, you can make complex comparisons. Full list of methods .
Date.create( 'June 15, 2002' ); > "Saturday, June 15, 2002 00:00"
Date.create( '2002-06-15' ); > "Saturday, June 15, 2002 00:00"
Date.create( '2002/06/15' ); > "Saturday, June 15, 2002 00:00"
Date.create( '15 June, 2002' ); > "Saturday, June 15, 2002 00:00"
Date.create( 'today' ); > "Monday, August 1, 2011 00:00"
Date.create( '2 days ago' ); > "Saturday, July 30, 2011 20:26"
Date.create(888888888899); > "Tuesday, March 3, 1998 04:34"
Date.create( 'the 15th of last month' ); > "Friday, July 15, 2011 00:00"
Date.create( 'the first day of 1998' ); > "Thursday, January 1, 1998 00:00"
Date.create().format( '{12hr}:{mm} {tt} on {Weekday}' ); > "8:26 pm on Monday"
Date.create( '3200 seconds ago' ).relative(); > "53 minutes ago"
Date.create().iso(); > "2011-08-01T16:26:20.122Z"
Date.create().isAfter( 'May 25' );> true
Date.create(). is ( 'tuesday' ); > false
Date.create(). is ( 'July' ); > false
Date.create(). is ( 'the 7th of June' ); > false
Date.create().addDays(2); > "Wednesday, August 3, 2011 20:26"
Date.create().addMonths(2); > "Saturday, October 1, 2011 20:26"
Date.create().addYears(2); > "Thursday, August 1, 2013 20:26"



Objects


Although Sugar does not directly interact with Object.prototype , the library provides several shortcuts to access the Object class method. One of them is Object.extended , which creates a hash object that has methods similar to those available in other languages. Also methods of type checking are added, such as isString , isFunction , etc. Full list of methods .
Object.extended({ broken: 'wear' }).keys(); > [ "broken" ]
Object.extended({ broken: 'wear' }).values(); > [ "wear" ]
Object.keys({ broken: 'wear' }); > [ "broken" ]
Object.clone({ broken: 'wear' }); > { "broken" : "wear" }
Object.isString( 'yes, it is' ); > true
Object.isFunction( function () {}); > true



Functions


In Sugar there are basic methods, such as bind , which allows you to define the scope of a function. There are also methods delay , defer and Function.lazy , which allows you to create closures for calculations that create a heavy load on the CPU. Full list of methods .
( function (a) {
/* this = 'wasabi', a = 'bobby' */
}).bind( 'wasabi' , 'bobby' )();
( function () {
/* delayed 500ms */
}).delay(500);
[1,2,3].each(Function.lazy( function () {
/* Each iteration will occur 5ms after the previous one */
}, 5));


* This source code was highlighted with Source Code Highlighter .

Useful links:

Source: https://habr.com/ru/post/125415/


All Articles