Skip to content

Instantly share code, notes, and snippets.

@Vigowebs
Vigowebs / walrus-operator.py
Created January 4, 2023 20:07
Walrus operator in python
numbers = [1, 2, 3, 4]
# Without the walrus operator
i = 0
while i < len(numbers):
print(numbers[i])
i += 1
# With the walrus operator
while (i := 0) < len(numbers):
@Vigowebs
Vigowebs / common-word-in-a-string.js
Created January 4, 2023 16:50
Find a common word in a string using JavaScript
function mostCommonWord(string) {
// Create a map to store the frequency of each word
const wordFrequency = new Map();
// Split the string into an array of words
const words = string.split(' ');
// Iterate over the array of words
for (const word of words) {
// If the word is not in the map, add it with a frequency of 1
@Vigowebs
Vigowebs / controller-action.php
Created January 2, 2023 14:51
This route will match a GET request to the /users URL and will call the index method of the UserController class. The index method retrieves a list of all users from the database using the User model and then returns a view file located at resources/views/users/index.blade.php, passing the $users variable to the view.
Route::get('/users', 'UserController@index');
class UserController extends Controller
{
public function index()
{
$users = User::all();
return view('users.index', compact('users'));
}
@Vigowebs
Vigowebs / generate-unique-id.js
Created December 11, 2022 17:18
Generate Unique id using JavaScript
const generateUniqueId = () => {
return Math.random().toString(36).substring(2)
}
generateUniqueId(); //7pvib9simcl
generateUniqueId(); //3aakkolzsu8
generateUniqueId(); //ligt7dtz83h
@Vigowebs
Vigowebs / eloquent-laravel-increament.php
Created December 5, 2022 19:20
Increments and Decrements in Eloquent Laravel
// ❌
$product = Product::find($production_id);
$product->stock++;
$product->save();
// ✔️
Product::find($production_id)->increment('stock'); // +1
@Vigowebs
Vigowebs / Intl-segmentor.js
Created December 4, 2022 04:32
The Intl.Segmenter object enables locale-sensitive text segmentation, enabling you to get meaningful items (graphemes, words or sentences) from a string. Currently Intl.Segmenter is not supported in Firefox and Firefox for Android browsers.
const segmenterEn = new Intl.Segmenter('en', { granularity: 'word' });
const string1 = 'Split JavaScript Strings!';
[...segmenterEn.segment(string1)]
// [
// {segment: 'Split', index: 0, input: 'Split JavaScript Strings!', isWordLike: true}
// {segment: ' ', index: 5, input: 'Split JavaScript Strings!', isWordLike: false}
// {segment: 'JavaScript', index: 6, input: 'Split JavaScript Strings!', isWordLike: true}
// ...
// ]
@Vigowebs
Vigowebs / config-api-key.php
Created November 26, 2022 05:03
Do not get data from the .env file directly. Pass the data to config files instead and then use the config() helper function to use the data in an application.
// ❌
$apiKey = env('API_KEY');
// ✔️
// config/api.php
'key' => env('API_KEY'),
@Vigowebs
Vigowebs / PHP-anagram.php
Created November 21, 2022 19:21
Write an anagram using PHP
function is_anagram($a, $b) {
return(count_chars($a, 1) == count_chars($b, 1));
}
//Example:
$a = 'Silent';
$b = 'Listen';
echo is_anagram($a,$b); // output: 1
@Vigowebs
Vigowebs / auto-complete-hint.php
Created November 2, 2022 18:28
Adding Auto-complete Hints to Artisan Commands
class TestCommand extends Command
{
public function handle()
{
$email = $this->anticipate('Find customer by email: ', [
'Gopi@vigowebs.com',
'Vigo@example.com',
]);
}
@Vigowebs
Vigowebs / get-or-put-collection.php
Created October 12, 2022 06:40
getOrPut() can be used to fetch an item if it already exists, or insert it and fetch it if it doesn't. This can be really useful when building up a Collection with data from multiple sources and don't want duplicate items in our data.
// ❌
if (! $collection->has($key)) {
$collection->put($key, $this->builtItem($data));
}
return $collection->get($key);
// ✔️
return $collection->getOrPut($key, fn () => $this->buildItem($data));